In this post I am going to show how easy it is to set up a service or script to start when a Linux server boots up.
The method I am going to demonstrate is using systemd
. I have a script monitor.sh
that simply runs a curl command in an endless while loop
.
This particular example does not do anything other than run the curl to monitor any url, but you could easily add a check and send alert if the curl statement fails.
The same monitor command could be run via cron as well, where you execute the script at a scheduled interval.
Configuring it via systemd
give you a bit more control over when the scripts starts on boot and also makes sure it keep running as long as the server is up.
This systemd
setup ensures that the script will always be active and there will be only one copy of it running at any given time.
If you did this via cron, you could end up with more than one active script in some cases.
Procedure
Let’s write our monitor.sh script first which runs the curl in an endless loop. Using an editor you are comfortable with, save the following code in a file /home/ubuntu/monitor.sh
Note: You can use any user that is available to you on the system. I am using ubuntu
in this example.
|
|
Make sure you have execute permission set.
|
|
Next, we will set up the systemd configuration file. Create a new file sudo vi /etc/systemd/system/mymonitor.service
.
Add the following content to the file.
|
|
Let’s examine this configuration.
- The
[Unit]
section is where we describe our service and the relationship of the unit to other units. - In this example, I want to ensure that my service runs after network is enabled
- In the
[Service]
section, I define that the process starts up the ubuntu user. If your script was creating some output files then the user and group will beubuntu:ubuntu
for the output file. - The ExecStart is where I set the path to my script.
- The next two lines ensure that my script is always running. If the job dies, then it will restart the script after a 5-second interval.
WantedBy=
ensures that the current service will be started when the listed services are started[multi-user.target]
Once the configuration file is saved, we reload the systemd daemon to ensure it picks up the new configuration file.
|
|
Also, the enable option makes the service start up on reboot. To start the service now you pass the start option.
Now if you run the ps command you can see your script is running as the ubuntu user.
|
|
You can also check the status of the service using the systemctl command.
|
|
You should see the service is active and running.
|
|
if you kill this process and run a ps again you will see that the process has been restarted, and it has a new pid.
|
|
Run journalctl -e
to see the logs of the service.
|
|
Conclusion
Once you understand the procedure, you can adapt the configuration to run any script, application or custom server. The systemd service will make sure your process is started on boot and always running.
Note: If you make any changes to your script monitor.sh
, make sure to stop and start your service so that a new process is launched.