3

This seems to come up several times, but I've been through all the answers and tried everything I can find and I still can't get a Python script to run at startup on my Kubuntu 18.04 system.

I've got a project setting up a datalogger for a home automation project. It's an ESP32 board running Micropython that sends MQTT messages back over the WiFi to save on the computer. I've managed to get it all working, I now simply need to start a Python script running on the computer to grab the MQTT messages. I can run it from terminal and it works perfectly. I assumed it would be trivial to get it to automatically run at startup so I don't have to type it in every time I switch the computer on, but I've spend hours on it and it still won't do it!! Things I've tried:

  1. Using the Autostart GUI in System Settings to run the command python /home/rosebank/datalogger/datalogger_server.py
  2. Writing a .sh file containing just the command python /home/rosebank/datalogger/datalogger_server.py, making it executable and using the Autostart GUI to run that script file
  3. Creating a .desktop file in ~/.config/autostart folder following these instructions: https://stackoverflow.com/questions/...05612#25805612 with the Python command
  4. As above but running the .sh script file
  5. Copying the .desktop file from above into /etc/xdg/autostart
  6. Setting 60 second delay on X-GNOME-Autostart-Delay in case the MQTT broker wasn't already running
  7. creating a .conf file and putting in etc/init
  8. Copying .py file into /bin and adding "@reboot python /bin/datalogger_server.py &" to the bottom of the crontab file

Literally nothing I try will make this script run!! I simply can't understand how it's this difficult, as I say all I need to do it type the command into Terminal after booting up and it works, so why can I not make it work automatically?! I don't know what's happening. Is the code not running at all? Is it trying to run and giving an error message? Is there anywhere I can find if there's any error messages it's generated? As I say, the code works fine just running it from terminal, so I can't see why it shouldn't work at boot.

EDIT: To clarify, on this computer it logs in automatically when it's switched on and the only 'user' is rosebank. So it's totally fine to have it run at log in rather than at boot. It's also fine to have it run by the user not by root as it just needs to be able to write into the user's home folder and doesn't need (probably shouldn't have) any special permissions. I really am just looking for an automatic way of doing exactly the same as what happens when I type the command into terminal.

4
  • 1
    All the solutions you have tried (except #7) are made for starting scripts when someone logs onto the computer and starts an interactive session. What you really need is to start the script when the computer boots. Consider adding the script to the list of services started with systemd. Commented Dec 28, 2019 at 10:57
  • I'd suggest to stick with the bash file. Make sure the file is executable and accessible (chmod 777 tends to do the trick). Make sure that the command python points to the correct python executable as well. Redirect the error/warning outputs into some log file (2> /error.log), so that you'll know what went wrong if something went wrong while executing the script. Commented Dec 28, 2019 at 11:00
  • @Richard Whatever you are hoping to accomplish, chmod 777 is wrong and dangerous. You will want to revert to sane permissions ASAP (for your use case, probably chmod 755) and if you have had world writable system files on a public-facing system, at the very least investigate whether it could have been breached and used as a pivot point for breaking into your organization’s network. Commented Dec 28, 2019 at 11:11
  • Running a script as root at system startup is significantly different from running it as yourself at the prompt. This is why autostarting GUI programs is tricky (you don't want root to have a GUI, but you also don't want to require someone to manually log in to start their personal GUI session) but it extends to many other system resources. If your script requires wifi, you need to separately make sure that wifi is up before you run, etc. Commented Dec 28, 2019 at 11:14

2 Answers 2

1

I faced the same problem when I run PyPI local server. I developed a local PyPI server and it should be run using hostingpypi runserver --port PORT --host HOST command.

So, borrowed systemd config file from https://pypi.org/project/pypiserver/#running-as-a-systemd-service and used it for running my script at Ubuntu startup.

Sign up to request clarification or add additional context in comments.

17 Comments

Thanks, that looks promising, if I change the 'ExecStart =' to point to my python script put the user as my username (see my edit, this computer only has one user and logs in automatically when it's turned on anyway) and set the working directory as ~/datalogger will that work?
It should work as long as the user have r/w permission to ~/datalogger. And if you use Ubuntu, there are many hidden default accounts you may not notice. They(hidden accounts) have minimal permission to your system to accomplish their work on background.
I'm happy to hear that. It's good to put conf files to /home/you/.config/, and save logs to /var/somewhere/. If your log file was cleaned up at every start up, you would probably do more work on logging for preserving old logs. Please show me logging part of your code. If this comment is too short for it, you may email/skype to me or post a new question.
First, try sudo pip install --update paho-mqtt, and do not forget to confirm python version you used in systemd script.
Installing packages using pip only will install for your account only. So www user may not access to them.
|
1

OK, so I finally sorted this with lots of help from Yunbo Sim. For anyone trying to do something similar and not having to read the full thread the summary of what worked was:

Things that tripped me up:

  • ExecStart needs to point to an actual command then the script you're trying to run, not just to the script, so for example to run a bash script in your home folder you need /bin/bash '~/script.sh' NOT just ~/script.sh; Or to run a Python script you need: /usr/bin/python '~/python_script.py' NOT just python ~/python_script.py

  • Not knowing you had to run the systemctl enable command to actually get it to run at startup (before I did that it would run when I started it but not automatically)

  • Needed to set it to After=mosquitto.service because my code relies on Mosquitto being running.

The other thing that tripped me up was having a version of Python that I had installed as part of Anaconda and was using to write all my code in which wasn't the same as the system wide install that was being run from the service that didn't include all the modules the script needed. To solve that either point the ExecStart command to the absolute path of the binary for the version of Python you want it to use or make sure the system wide version has the right modules installed.

3 Comments

The single quotes around the script names definitely look wrong. If the script has correct permissions and a proper shebang line, you should not need to explicitly specify the interpreter in the ExecStart entry.
It does work this way. Part of the problem I was having was that I had written the code with Python3.7 installed via Anaconda, which had several packages it needed. The command python in a terminal window would run Python3.7, but the service file was using a system Python 3.6 install that didn't have all the packages. So I was running ExecStart= ~/anaconda3/bin/python 'myscipt.py' to make sure it used the version with all the packages installed.
I later installed the relevant packages in the system Python3.6 install, possibly now I don't need to specify explicitly where to find Python, but it is working this way and the last thing I want to do now is waste any more time on this! I really thought that making this run at startup would be a 5 minute job I can't believe how much time I've spent on this, I had never even heard of a .service file when I started this!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.