1

I wanted to run a python script at boot in Ubuntu. I tried referring to few existing solutions in stackoverflow but somehow I don't get it working. Could anyone advise me on what piece of info I am missing!


Reference 1

Run Python script at startup in Ubuntu answered by @RickyA

Put this in /etc/init (Use /etc/systemd in Ubuntu 15.x)

mystartupscript.conf

start on runlevel [2345]
stop on runlevel [!2345]
exec /path/to/script.py

By placing this conf file there you hook into ubuntu's upstart service that runs services on startup.

manual starting/ stopping is done with sudo service mystartupscript start and sudo service mystartupscript stop

Results for Reference 1

In my case, the python script that I wanted to run on boot is located in /home/aspma/Dropbox/Linux/c_lynda_program_backup_script.py

When I tried to manually start the service from terminal window, i got an error message: start Job failed to start

Screenshot of mystartupscipt.conf file that i added inside /etc/init and its failed attempt for manual starting the service


Reference 2

How To Start Python Script On Bootup answered by @Germar

You can add it to /etc/rc.local. This can be used to run scripts and programs on system boot which doesn't have their own scripts for runlevels. It will run as root

Run sudo nano /etc/rc.local and add your line before exit 0

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

python /home/my-sickbeard-install/SickBeard.py 2>&1 >/dev/null &

exit 0

Results for Reference 2

In my case, the python script that I wanted to run on boot is located in /home/aspma/Dropbox/Linux/c_lynda_program_backup_script.py

Screeenshot of file that is edited in /etc/rc.local

This doesn't work too


Virtual Machine Details

Ubuntu Version 14.04.5 LTS

Python 2.7.6

prasanth_ntu
  • 19
  • 1
  • 7

1 Answers1

0

I managed to get the python script run automatically at the boot, by adding one more step to the solution given in Reference 2 (see the question).

By adding the directory of the python script to PYTHONPATH, I was able to get it working.

To add the directory permanently to the PYTHON PATH, add this to your ~/.bashrc

export PYTHONPATH=$PYTHONPATH:/my/other/folder_containing_python_script

Thanks to the post: Permanently add a directory to PYTHONPATH

Community
  • 1
  • 1
prasanth_ntu
  • 19
  • 1
  • 7