How can I run script when I start up my Linux machine?

4

1

I would like to run the python script, when I startup the Linux machine. How can I do so? I don't need the user to login the OS to start the script. (The user is password protected.)

Ted Wong

Posted 2012-08-23T12:41:01.320

Reputation: 903

3What distribution? Are you using upstart, systemd, or something else? – David Schwartz – 2012-08-23T12:42:42.487

1Have you done much research into this? The top results when Googling your question comes up with links that say the exact same thing as the below answer from mnmnc. – bobmagoo – 2012-08-23T12:58:07.157

possible duplicate of How do I make a script run upon startup of the Ubuntu machine?

– Indrek – 2012-08-23T17:46:56.953

@Indrek: It's not an exact duplicate, since this post does not specify Ubuntu, while the question is somewhat distro-specific. – user1686 – 2012-08-23T18:50:53.973

Answers

5

Generic method – add the script to /etc/rc.local:

/usr/bin/python pythonscript.py

if your python interpreter is under /usr/bin, or simply

/path/to/pythonscript.py

if your script is marked executable (+x).

Note that some systems use /etc/rc.d/rc.local instead.


On systemd systems, rc.local might be ignored completely.

Create a service unit, /etc/systemd/system/something.service:

[Unit]
Description=Script name

[Service]
ExecStart=/path/to/script.py

[Install]
WantedBy=multi-user.target

Tell systemd to start the script on boot with systemctl enable something.service.

mnmnc

Posted 2012-08-23T12:41:01.320

Reputation: 3 637

2Although Ted explicitly asks about OS startup and not about user login, the proper place for post-login scripts is .profile, .xprofile and/or .config/autostart/. Adding something to .bashrc will cause it to be run every time a terminal is opened. – user1686 – 2012-08-23T17:15:55.013

2Just to note: on some systems, the file may be located at /etc/rc.d/rc.local. – clpo13 – 2012-08-23T17:44:54.463

1@clpo13: And some systems might not be using rc.local at all (such as most systemd-using Linux distros)... – user1686 – 2012-08-23T17:51:17.367