How do I make a python script (including its dependencies) run on startup of an Ubuntu machine?

Posted on

Problem :

Well, I do know that for a single script the question is to just put the corresponding file in directory /etc/rc.local, right? But, what if this script depends on other files (i.e. a few scripts and a couple of text files)? What should I do in order to avoid making this certain folder a mess?

Solution :

If by depends, you mean it uses and/or calls them, that should not matter. You can just add a line to the end of the rc.local file as a command to call the main script and it should just work.

There is an alternative to rc.local, atleast on desktop versions. You can use the Startup Applications app to add your script to it. The difference between the two methods, other than CLI vs GUI version of adding is that rc.local scripts are run as ROOT and the startup applications as your user.


From the comments, your problem becomes clearer. It seems that the script is using relative paths to access the dependency, which I’m guessing isn’t code. The working directory of the above two methods are either /root/ or /home/<user name/, thus the dependencies dont load.

Easiest fix is to cd to the folder before invoking the python script. Either of these should work:
– Use cd /home/..../.../ && python as the command in either of those two methods.
– If that doesnt work, make a script(.sh should do) which calls the above command and call this script in the above 2 methods.

The more elegant way of doing it is
– Do the cd from inside the python script by programatically determining its path
– Or Use the above path to access the dependencies by absolute path


Ubuntu might have moved away from rc.local in favor of upstart. Try to see if using upstart as in this question works for you.

If you have cron enabled (which you should) there is a special @reboot command which runs on startup. I am unsure of this works on cold boots though, but from what I can see it does.

If it works, you just write a script, plonk it in some clean and organised directory, then call it through cron.

See: Here and Here

Leave a Reply

Your email address will not be published. Required fields are marked *