Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / Linux / How to Manage Services & Scripts Startup on CentOS/RHEL?

April 19, 2023 CentOSLinuxQuestions and AnswersRHEL

How to Manage Services & Scripts Startup on CentOS/RHEL?

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.

Contents:
  • Using Systemctl to Manage Systemd Services in Linux
  • Run Script or Service with Rc.local
  • How to Create a Linux Service with Systemd?
  • How to Run Scripts Using Cron?
  • Bash Startup Scripts: .bashrc

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:

systemctl list-units

systemctl list-units linux centos

You can get the list of unit files using this command:

systemctl list-unit-files

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
[email protected] 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

Examples:

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

systemctl list-units —type=service - list loaded daemons in linux

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
The output of the command shows the directory, in which the symlink to the service file has been created.

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 the startup of 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:

systemctl masked services

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

using rc.local to run command on linux stratup

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:

sh /root/test.sh

add sh script to rc.local

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:

touch /etc/systemd/system/test-script.service
chmod 664 /etc/systemd/system/test-script.service
nano /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 the 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 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

bashrc run script at startup

Then I saved the file and restarted the SSH session:

Bash Startup Commands

As you can see, when starting the terminal, the webserver has also been restarted. What actions can be performed when starting the terminal? It may be some extra tools, like uptime server check:

show uptime on ssh connect

Or if you want to get to the specific directory and start mc when running the ssh console, add the following to .bashrc:

cd /var/
mc

bashrc change directory and run program

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 distros). I am sure that this information will be handy for the people studying the basics of Linux system administration.

0 comment
1
Facebook Twitter Google + Pinterest
previous post
How to Clear RDP Connections History in Windows?
next post
Keepalived: Configuring High Availability with IP Failover on CentOS/RHEL

Related Reading

How to Increase Size of Disk Partition in...

October 5, 2023

How to Use Ansible to Manage Windows Machines

September 25, 2023

Fixing ‘The Network Path Was Not Found’ 0x80070035...

August 30, 2023

How to Install and Configure Ansible on Linux

August 27, 2023

Computer Doesn’t Turn Off After Shutting Down Windows...

August 26, 2023

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023
  • How to Use Ansible to Manage Windows Machines

    September 25, 2023
  • Installing Language Pack in Windows 10/11 with PowerShell

    September 15, 2023
  • Configure Email Forwarding for Mailbox on Exchange Server/Microsoft 365

    September 14, 2023
  • How to View and Change BIOS (UEFI) Settings with PowerShell

    September 13, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • How to Configure MariaDB Master-Master/Slave Replication?
  • How to Mount Google Drive or OneDrive in Linux?
  • KVM: How to Expand or Shrink a Virtual Machine Disk Size?
  • Install and Configure SNMP on RHEL/CentOS/Fedor
  • Configuring High Performance NGINX and PHP-FPM Web Server
  • Adding VLAN Interface in CentOS/Fedora/RHEL
  • Configuring Routing on Linux (RHEL/CentOS)
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top