4

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
  • 933
  • 8
  • 14
  • 3
    What distribution? Are you using `upstart`, `systemd`, or something else? – David Schwartz Aug 23 '12 at 12:42
  • 1
    Have 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 Aug 23 '12 at 12:58
  • possible duplicate of [How do I make a script run upon startup of the Ubuntu machine?](http://superuser.com/questions/155476/how-do-i-make-a-script-run-upon-startup-of-the-ubuntu-machine) – Indrek Aug 23 '12 at 17:46
  • @Indrek: It's not an exact duplicate, since this post does not specify Ubuntu, while the question is somewhat distro-specific. – u1686_grawity Aug 23 '12 at 18:50

1 Answers1

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.

u1686_grawity
  • 426,297
  • 64
  • 894
  • 966
mnmnc
  • 4,011
  • 1
  • 20
  • 24
  • 2
    Although 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*. – u1686_grawity Aug 23 '12 at 17:15
  • 2
    Just to note: on some systems, the file may be located at `/etc/rc.d/rc.local`. – clpo13 Aug 23 '12 at 17:44
  • 1
    @clpo13: And some systems might not be using `rc.local` at all (such as most *systemd*-using Linux distros)... – u1686_grawity Aug 23 '12 at 17:51