NoCache

Table of Contents

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

Cyrus Kao
Last modified on .

There are plenty of ways to automatically run a script or command at Linux boot up and after reboot, either by using cron, rc.local or my preferred method — systemd. It creates services which are easier to manage. You can start/stop/enable/disable them with systemctl after they're set up. Furthermore, services can even restart themselves after startup failure or crash.

Create Service

Create the unit configuration file /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
    
  • Nano (easy to use command-line editor)

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

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

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

/etc/systemd/system/your-service.service[Unit]
Description=YourService

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

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

Auto Restart (Optional)

To tell the service to restart itself after failure, append Restart=always under the Service section:

/etc/systemd/system/your-service.service[Service]
...
Restart=always
Plain text

Acceptable values of the Restart directive include no, on-success, on-failure, on-abnormal, on-watchdog, on-abort and always.

It's also a good idea to set RestartSec to configure the amount of time to sleep before restarting the service.

/etc/systemd/system/your-service.service[Service]
...
RestartSec=5
Plain text

Set Environment (Optional)

To set the environment variables:

/etc/systemd/system/your-service.service[Service]
...
Environment="production=true"
Plain text

Set Working Directory (Optional)

To set working directory:

/etc/systemd/system/your-service.service[Service]
...
WorkingDirectory=/home/user
Plain text

Enable Service

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

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

Remember to reload the daemon after the systemd unit files are changed:

$ sudo systemctl daemon-reload

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

$ sudo systemctl enable your-service

Verify It's Working

To verify the service is working, start the service and check its status:

$ sudo systemctl start your-service
$ systemctl status your-service
○ 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

Since mine is a simple echo "Hello World" script, it'll be considered as inactive once executed.

Disable Service

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

$ sudo systemctl diable your-service

Stop Immediately

Disabled services can still be running in the background, to stop the service immediately:

$ sudo systemctl stop your-service

Comments

Sign in to leave a comment.