How to run python on linux Mint

Asked

Viewed 677 times

1

I made a program in python and I would like to start it next to the system, but I don’t know how. I tried cron @reboot, but I was unsuccessful.

I’m not sure how ~/. bashrc works did so.

nano ~/.bashrc 

inside him I put.

alias opentray='python /usr/share/trayMenuCode/trayMenuCode.py'  

unsuccessful. maybe it doesn’t even make sense.

img
    imagens.png
    ....
tray.db
trayMenuCode.py 

someone could give a light.

  • 3

    The .bashrc only runs when vc starts a bash session with your user (and if it opens multiple sessions, it runs several times, one for each session), and the alias only serves to create a shortcut (a shorter command, so you don’t have to type the longer command). Maybe that’s what you want: https://unix.stackexchange.com/q/56957

1 answer

3

Hey, buddy, good morning.

Dude, in bashrc you will only change the routine when you open the terminal.

Step 1:

Crie seu arquivo .py onde quiser, exemplo: "/home/meuscript.py"

Step 2: Create a file with name:

sudo nano /lib/systemd/system/meuscript.service

or

sudo touch /lib/systemd/system/meuscript.service

NOTE: It does not matter how you create the file, but distros based on Ubuntu/Debian will use nano as default, but in case do not have or is another type of distribution, use the same touch.


Step 3: After creating the file by following step 2, copy and paste the code below into the file you created

[Unit]
Description=My Script Service
After=multi-user.target

[Service]
Type=idle
ExecStart=/usr/bin/python /home/meuscript.py

[Install]
WantedBy=multi-user.target

Note: in Execstart=/usr/bin/python /home/meuscript.py you change by the path from where you created your python script.

OBS 2: AND IN "My Script Service" you change to the name of the service you want your script to be called.

OBS 3: "Execstart" is the command that the system will start when connecting, but if you want to add some more parameter after the name of the script, there it is with yourself.

If you want to store the log output of the script in a file, you can do it as follows on "meuscript.service":

ExecStart=/usr/bin/python /home/meuscript.py > /home/meuscript.log 2>&1

NOTE: you can save the logs in /var/logs also.


Step 4: Give the file permission:

sudo chmod 644 /lib/systemd/system/meuscript.service

Step 5: Enable the file as a service in systemd:

sudo systemctl daemon-reload && sudo systemctl enable meuscript.service

I hope it helps, man. Big hug.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.