systemd start scripts or applications as daemons

Daemons

You can easily start your scripts or applications as daemons.
A daemon is an application that runs as a "background" process (without a terminal or user interface).

Systemd

Starting with V2.x of our Linux BSP we use systemd as our init and service manager.
systemd is a system and service manager for Linux, also capable of replacing the traditional SysV init system.
http://www.freedesktop.org/wiki/Software/systemd/ 
http://www.freedesktop.org/software/systemd/man/systemd.html 
unit configuration file whose name ends in .service encodes information about a process controlled and supervised by systemd.
Service files can be found in /etc/systemd/system/ and, for distribution provided ones in /lib/systemd/system/. Services can be started or permanently enabled using the systemctl command.

The common configuration items are configured in the generic "[Unit]" and "[Install]" sections. The service specific configuration options are configured in the "[Service]" section.
Service files must include a "[Service]" section, which carries information about the service and the process it supervises.
For more information on options of [Service] section refer to: http://www.freedesktop.org/software/systemd/man/systemd.service.html#Options 
  • Procedure
Create a unit configuration file ending in .service
Copy the unit configuration file to "/etc/systemd/system" and use the systemctl tool for starting or permanently enabling the service.
  • Using the systemctl command
From: http://www.freedesktop.org/software/systemd/man/systemctl.html 
Reload the systemd configuration and unit files:
root@colibri-t20:~# systemctl --system daemon-reload
Check status, start and stop a service, valid until next reboot:
root@colibri-t20:~# systemctl status <service_name>.service
root@colibri-t20:~# systemctl start <service_name>.service
root@colibri-t20:~# systemctl stop <service_name>.service
Add a service to or remove it from the ones started at boot. Note that this neither starts or stops the service but takes only effect during the next boot:
root@colibri-t20:~# systemctl enable <service_name>.service
root@colibri-t20:~# systemctl disable <service_name>.service
Example unit configuration file to automatically execute the (hypothetical) mydatalogger application at start up:
[Unit]
Description=mydatalogger service, collects and logs data in the background
After=multi-user.target
 
[Service]
Type=simple
ExecStart=/usr/bin/mydatalogger
 
[Install]
WantedBy=multi-user.target

Shells

/etc/profile

Each time a login shell is spawned the script /etc/profile plus all scripts in /etc/profile.d are executed. This is done for logins over a serial line, over an ssh connection and also for logins in the display manager to a graphical desktop. 
/etc/profile is sourced upon login: it sets up the environment upon login and application-specific settings by sourcing any readable file in /etc/profile.d/.
Using /etc/profile is well suited to set the environment or to do some small tasks. Note that these scripts must return control in order to continue with the login.

Remove the file /etc/profile.d or the additions to /etc/profile in order to undo the automatic execution.
  • Procedure
Add a script file in /etc/profile.d/*.sh.
Example script file:
#!/bin/sh
#script file for deleting backup entries
rm /home/root/*~

Comments

Popular posts from this blog

HAproxy logging

tomcat catalina coyote jasper cluster

NFS mount add in fstab _netdev instead of default | firewall-cmd --list-all