How to Run Script/Program at Startup with Systemd on Linux

mail
Cyrus Kao

There are plenty of ways to automatically run a script or command at Linux boot up, either by cron, rc.local or my preferred way — systemd. Services are easier to manage, you can start/stop/enable/disable it with systemctl on the fly, it can even restart itself after failure.

Create Service

Create /etc/systemd/system/your-service.service with the text editor of your choice:

  • Vim (command-line editor for advanced user)

    sudo vim /etc/systemd/system/your-service.service
    Bash
  • Nano (easy to use command-line editor)

    sudo nano /etc/systemd/system/your-service.service
    Bash
  • Gedit (editor with graphical interface, default text editor of Ubuntu and distros with GNOME)

    sudo touch /etc/systemd/system/your-service.service
    gedit admin:/etc/systemd/system/your-service.service
    Bash

Then add the following content to your-service.service:

[Unit]
Description=YourService

[Service]
ExecStart=/path/to/your/script.sh

[Install]
WantedBy=multi-user.target
Plain text
Service example
Service example in gedit

Auto Restart (Optional)

To restart the service automatically:

[Service]
...
Restart=always
Plain text

Acceptable values including no, on-success, on-failure, on-abnormal, on-watchdog, on-abort and always.

Set Environment (Optional)

To set environment variables:

[Service]
...
Environment="production=true"
Plain text

Set Working Directory (Optional)

To set working directory:

[Service]
...
WorkingDirectory=/home/user
Plain text

Enable Service

Before enabling your service, change the permissions of your-service.service to 664:

sudo chmod 644 /etc/systemd/system/your-service.service
Bash

Remember to reload the daemon after the service files being changed:

sudo systemctl daemon-reload
Bash

Then enable the service so it will run at system startup:

sudo systemctl enable your-service
Bash

Verify It's Working

To verify the service is working, start the service and check its status (since mine is a simple echo "Hello World" script, it'll be considered inactive once executed):

sudo systemctl start your-service
systemctl status your-service
Bash
○ greet.service - Greet
     Loaded: loaded (/etc/systemd/system/greet.service; enabled; vendor preset: disabled)
     Active: inactive (dead) since Thu 2022-02-17 15:12:19 CST; 3s ago
    Process: 7810 ExecStart=/greet.sh (code=exited, status=0/SUCCESS)
   Main PID: 7810 (code=exited, status=0/SUCCESS)
        CPU: 3ms

Feb 17 15:12:19 linux systemd[1]: Started Greet.
Feb 17 15:12:19 linux greet.sh[7810]: Hello World
Feb 17 15:12:19 linux systemd[1]: greet.service: Deactivated successfully.
Output

Disable Service

To disable the service if you don't want it to run at system startup anymore:

sudo systemctl diable your-service
Bash

Stop Immediately

To stop the service immediately:

sudo systemctl stop your-service
Bash
account_circle
I'm a full stack developer and GNU/Linux enthusiast from Taiwan. BTW, I use Arch.

Comments

0500