banner



How To Autostart Service Account Is Redhat Linux

In this commodity, we'll learn the basics of how to configure services and scripts to start automatically in Linux CentOS/RHEL 7/8. In detail, nosotros'll get acquainted with systemd daemon, learn how to add services to or remove them from startup, and consider the culling ways of starting scripts or daemons on boot in Linux.

The article is aimed at didactics you to rapidly 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/organization (init.d used scripts from /etc/init.d/). Systemd allows you to parallelize service startup on OS boot.

To manage the systemd, the systemctl control is used.

First of all, later on booting the system we'll check the list of units available in systemd:

systemctl list-units

systemctl list-units linux centos

You tin become the list of unit of measurement files using this command:

systemctl list-unit-files

This control volition display all available unit of measurement files.

To display the list of active services and their states, run this command:

# systemctl listing-units -t service

Since some units may get inactive afterwards the startup, you tin become the full list using the —all option.

# systemctl list-units --all

UNIT LOAD ACTIVE SUB DESCRIPTION proc-sys-fs-binfmt_misc.automount loaded agile waiting ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ● exim.service non-found inactive expressionless exim.service firewalld.service loaded active running firewalld - dynamic firewall daemon getty@tty1.service loaded active running Getty on tty1 ● iptables.service not-found inactive dead iptables.service Bring up/downwardly networking ● NetworkManager-wait-online.service not-plant inactive dead        

As you can meet from the list, even the services not found on the disk are displayed.

Also, you tin 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 --country=active —display the list of active systemd units but

systemctl listing-units —blazon=service — display the listing 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 control adds the nginx spider 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/organisation/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 non started automatically, y'all will see disabled here.

How to Disable a Service in Systemd?

You can remove a service from startup and so it is not started on Linux kicking (while the service itself is non 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/organisation/multi-user.target.wants/nginx.service

Afterwards 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 accept run across some wicked services that take still stayed in the startup afterward disabling them and started after the Linux reboot. To solve this problem, yous can mask a service:

# systemctl mask nginx.service

And then information technology won't outset either manually or after the OS restart:

# systemctl mask nginx.service

Created symlink from /etc/systemd/organisation/nginx.service to /dev/null.

# service nginx restart

Redirecting to /bin/systemctl restart nginx.service Failed to restart nginx.service: Unit of measurement is masked.        

You can unmask a service using this command:

# systemctl unmask nginx.service

Removed symlink /etc/systemd/organization/nginx.service.

If after masking a service you check your unit files, you volition run across that the service is marked as masked:

systemctl masked services

Run Script or Service with Rc.local

To run unlike scripts on Linux kick, rc.local is oft used.

Besides scripts, using rc.local you lot can run services every bit well, even those that are started using systemd. I do not know why y'all should utilize rc.local, if there is systemd, merely here is a couple of examples.

Starting time 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 tin can add a control to start nginx web server to rc.local:

service nginx start

using rc.local to run command on linux stratup

But I seldom utilize rc.local to starting time services. More often rc.local is used to showtime a script or run a command once.

For example, I have created a script /root/test.sh that does something and I desire to run information technology right later boot. Add the following line to the rc.local file:

sh /root/examination.sh

add sh script to rc.local

Starting with CentOS 7, the developers point out that rc.local is an obsolete daemon and information technology is not recommended to use it to start scripts or services. But I utilize it while information technology is nevertheless working because it is very elementary.

How to Create a Linux Service with Systemd?

You can create your own daemon and manage it via systemd.

For example, y'all want to get-go the same script (/root/test.sh) each time the system is rebooted. Let's begin with creating a file of our new service:

touch on /etc/systemd/system/test-script.service
chmod 664 /etc/systemd/organisation/examination-script.service
nano /etc/systemd/organisation/test-script.service

Here is the contents of the file:

[Unit of measurement] 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 procedure to end before continuing on with other units

Check and restart information technology:
# systemctl daemon-reload
# systemctl start test-script.service
# systemctl condition test-script.service

● examination-script.service - Test Loaded: loaded (/etc/systemd/system/test-script.service; disabled; vendor preset: disabled) Active: agile (running)        

If the service works well, add together information technology 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 lot can add any script to autostart and manage them through systemd.

How to Run Scripts Using Cron?

If y'all desire to run a script or a command at some frequency, you can use cron:

crontab -e — opens an editor to alter a cron task table

And add together a task you want here, for instance:

* * * * * /root/test.sh — to run a script one time a minute.

You tin write a watch-domestic dog script that will check the service status, and if the service is non running, the script volition start it. I'm using a like way in some of my projects.

To brandish the list of all tasks in cron, run the control:

# crontab -l

* * * * * /root/test.sh

The bachelor time values to run cron tasks in order:

  • Minutes: 0-59
  • Hours: 0-59
  • 24-hour interval of a calendar month: i-31
  • Month: 1-12
  • Twenty-four hours of a calendar 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 too identify the script in one of cron directories:

  • /cron.daily – for a script run once a twenty-four hours
  • /cron.hourly – for a script run once an hr
  • /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 desire to perform some actions when starting the SSH panel, you tin can add any command or script to .bash_profile or .bashrc file. In theory, you can add activity into whatever of these files, it will be run in whatsoever 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 encounter, when starting the concluding, the webserver has also been restarted. What actions can exist performed when starting the terminal? It may exist some extra tools, similar uptime server check:

show uptime on ssh connect

Or if yous want to go to the specific directory and start mc when running the ssh panel, add the following to .bashrc:

cd /var/
mc

bashrc change directory and run program

Hopefully, this article nigh how to manage Linux service or script startup in Linux has been helpful to you (this article was written for CentOS and RHEL, simply suitable for other distros). I am sure that this information will be handy for the people studying the basics of Linux system assistants.

Source: http://woshub.com/manage-services-scripts-startup-centos-rhel/

Posted by: harrellgare1973.blogspot.com

0 Response to "How To Autostart Service Account Is Redhat Linux"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel