In this article we’ll learn the basics of how to configure services and scripts to start automatically in Linux CentOS/RHEL 7/8. In particular, we’ll get acquainted with systemd daemon, learn how to add services to or remove them from startup, and consider the alternative ways of starting scripts or daemons on boot in Linux.
The article is aimed at teaching you to quickly find the list of services or scripts started automatically in Linux, add your services or scripts to startup or disable automatic startup of some apps.
Using Systemctl to Manage Systemd Services in Linux
Most popular Linux distros (CentOS, RHEL, Debian, Fedora and Ubuntu) use systemd startup daemon instead of init.d. Systemd is a Linux service manager used to start other daemons and manage them. It uses unit files from
/etc/systemd/system (init.d used scripts from
/etc/init.d/). Systemd allows you to parallelize service startup on OS boot.
To manage the systemd, the systemctl command is used.
First of all, after booting the system we’ll check the list of units available in systemd:
You can get the list of unit files using this command:
This command will display all available unit files.
To display the list of active services and their states, run this command:
# systemctl list-units -t service
Since some units may become inactive after the startup, you can get the full list using the —all option.
# systemctl list-units --all
UNIT LOAD ACTIVE SUB DESCRIPTION proc-sys-fs-binfmt_misc.automount loaded active waiting ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ● exim.service not-found inactive dead exim.service firewalld.service loaded active running firewalld - dynamic firewall daemon firstname.lastname@example.org loaded active running Getty on tty1 ● iptables.service not-found inactive dead iptables.service Bring up/down networking ● NetworkManager-wait-online.service not-found inactive dead
As you can see from the list, even the services not found on the disk are displayed.
Also, you can use some other flags, for example:
- —state — used to detect the daemon state: Load, Active, Sub
- —type — allows to filter units by type
systemctl list-units --all --state=active —display the list of active systemd units only
systemctl list-units —type=service — display the list of units that are the services
How to Create a Service in Systemd?
To manage services, systemd is using a special syntax. You must add .service after the name of a service. For example:
# systemctl enable nginx.service – the command adds the nginx web server to startup
This command will create a symbolic link to a file specified in the service command in systemd startup directory.
# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service
In order to check if a service has been added to startup, you can get its status:
systemctl status nginx.service
Note the following line in the output:
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
The enabled value means that this service is added to Linux startup. If the service is not started automatically, you will see disabled here.
How to Disable a Service in Systemd?
You can remove a service from startup so it is not started on Linux boot (while the service itself is not removed). To disable startup of a service, run the following command:
# systemctl disable your_service
For example, to disable nginx autostartup:
# systemctl disable nginx.service
Removed symlink /etc/systemd/system/multi-user.target.wants/nginx.service
After doing it, the symlink to a service file will be removed from systemd directory. You can make sure if the service is started automatically:
# systemctl is-enabled nginx
How to Mask Units with Systemd?
I have come across some wicked services that have still stayed in the startup after disabling them and started after the Linux reboot. To solve this problem, you can mask a service:
# systemctl mask nginx.service
Then it won’t start either manually or after the OS restart:
# systemctl mask nginx.service
Created symlink from /etc/systemd/system/nginx.service to /dev/null.
# service nginx restart
Redirecting to /bin/systemctl restart nginx.service Failed to restart nginx.service: Unit is masked.
You can unmask a service using this command:
# systemctl unmask nginx.service
Removed symlink /etc/systemd/system/nginx.service.
If after masking a service you check your unit files, you will see that the service is marked as masked:
Run Script or Service with Rc.local
To run different scripts on Linux boot, rc.local is often used.
Besides scripts, using rc.local you can run services as well, even those that are started using systemd. I do not know why you should use rc.local, if there is systemd, but here is a couple of examples.
First of all, /etc/rc.local must be executable:
chmod +x /etc/rc.local
Rc.local must be added to systemd autostart:
systemctl enable rc-local
And we can add a command to start nginx web server to rc.local:
service nginx start
But I seldom use rc.local to start services. More often rc.local is used to start a script or run a command once.
For example, I have created a script /root/test.sh that does something and I want to run it right after boot. Add the following line to the rc.local file:
Starting with CentOS 7, the developers point out that rc.local is an obsolete daemon and it is not recommended to use it to start scripts or services. But I use it while it is still working because it is very simple.
How to Create a Linux Service with Systemd?
You can create your own daemon and manage it via systemd.
For example, you want to start the same script (/root/test.sh) each time the system is rebooted. Let’s begin with creating a file of our new service:
chmod 664 /etc/systemd/system/test-script.service
Here is the contents of the file:
[Unit] Description=Template Settings Service After=network.target [Service] Type=oneshot User=root ExecStart=/root/test.sh [Install] WantedBy=multi-user.target
The main parameters are:
User – a user account the daemon is started from
Type=oneshot — the systemd should wait for the process to end before continuing on with other units
Check and restart it:
# systemctl daemon-reload
# systemctl start test-script.service
# systemctl status test-script.service
● test-script.service - Test Loaded: loaded (/etc/systemd/system/test-script.service; disabled; vendor preset: disabled) Active: active (running)
If the service works well, add it to systemd startup :
# systemctl enable test-script.service
Created symlink from /etc/systemd/system/multi-user.target.wants/test-script.service to /etc/systemd/system/test-script.service.
Thus, you can add any script to autostart and manage them through systemd.
How to Run Scripts Using Cron?
If you want to run a script or a command at some frequency, you can use cron:
crontab -e — opens an editor to change a cron task table
And add a task you want here, for example:
* * * * * /root/test.sh — to run a script once a minute.
You can write a watch-dog script that will check a service status, and if the service is not running, the script will start it. I’m using a similar way in some of my projects.
To display the list of all tasks in cron, run the command:
# crontab -l
* * * * * /root/test.sh
The available time values to run cron tasks in order:
- Minutes: 0-59
- Hours: 0-59
- Day of a month: 1-31
- Month: 1-12
- Day of a week: 0-7 (0 or 7 is Sunday)
In our task, the script is run once a minute, so there are *asterisks* there.
You can also place the script in one of cron directories:
- /cron.daily – for a script run once a day
- /cron.hourly – for a script run once an hour
- /cron.monthly — for a script run once a month
- /cron.weekly — for a script run once a week
The scripts in the specified directories will be run according to the automatic schedule.
Bash Startup Scripts: .bashrc
If you want to perform some actions when starting the SSH console, you can add any command or script to .bash_profile or .bashrc file. In theory, you can add an action into any of these files, it will be run in any case. Usually, the things you need are added to .bashrc, and .bashrc is started from .bash_profile.
I added a command to restart nginx web service to the .bashrc file:
service nginx restart
Then I saved the file and restarted the SSH session:
As you can see, when starting the terminal, the web server has also been restarted. What actions can be performed when starting the terminal? It may be some extra tools, like uptime server check:
Or if you want to get to the specific directory and start mc when running the ssh console, add the following to .bashrc:
Hopefully, this article about how to manage Linux service or script startup in Linux has been helpful to you (this article was written for CentOS and RHEL, but suitable for other distos). I am sure that this information will be handy for the people studying the basics of Linux system administration.