sections in this module City College of San Francisco - CS260A
Linux System Administration

Module: StartupShutdown III
module list

Customizing the boot process in RH7

If there is a startup item that you need to add to the boot process, you can make use of the fact that system5 subsystems still work in RH7 - they are just not recommended. You can thus use the same technique as in RH6.5.

The better way to proceed is to create a systemd service unit to perform your task. How you proceed is dependent on the type of your startup item. Your unit file should be placed in /etc/systemd/system rather than /usr/lib/systemd/system. The /etc location is where local units go.

in both of these cases, you must set Restart= to an appropriate value if you want the service to be restarted if it exits. You may also need to set RestartSec.

You will have to configure your unit's dependencies and, probably, link it to start automatically. The most common link is to be started when the system goes to multi-user mode, which requires a WantedBy=multi-user.target line in the Install section. (Unlike system5 init, systemd goes through multi-user mode to get to graphical mode, i.e.,  graphical.target has a Requires and a Before dependency on multi-user.target.) The Before= and After= dependencies require the most customization. Find a service unit that is similar and examine its dependencies. If you just want the service to be started by hand, no WantedBy dependency should be added.

Once you have the service unit file, you should do a test start and stop to make sure systemd is starting and stopping it correctly. When the service unit seems to work correctly, you must enable it if you want it to be started automatically. (This assumes you have configured a WantedBy dependency to make your unit start with the unit that wants it.)

Example:

Here's a simple script that just generates some simple statistics to a log file:

# cat /usr/local/bin/somecmds
#!/bin/bash
# relies on systemd reading the environment file to
# define the LOGFILE variable. add a default here
LOGFILE=${LOGFILE:-/var/log/somecmds.log}
# systemd is responsible for running these commands at
# the appropriate interval
exec >> "$LOGFILE"
dt=$(date "+%D %R")
us=$(users)
up=$(uptime)
echo "$dt: users=$us: $up"

Notice that this program writes its single message to stdout, which has been redirected (using append) to $LOGFILE. LOGFILE is given a default location.

Here is a service unit that runs our program. The service file is in /etc/systemd/system and is named trackusers.service:

# more trackusers.service
[Unit]
Description=Simple system monitoring service

[Service]
Restart=on-success
RestartSec=5min
ExecStart=/usr/local/bin/somecmds
Environment="LOGFILE=/var/log/trackusers.log"
Type=simple

[Install]
WantedBy=multi-user.target

Notice that the service is set to restart every 5 minutes so long as its last execution succeeds. It runs our shell script. It has an environment variable set for somecmds via an Environment= line. This sets the LOGFILE path (avoiding the default assignment in somecmds). We have enabled it using systemctl enable trackusers.service, so that it will be started by multi-user.target.

When we reboot the system, we waited for its log file to have at least two lines so that we could see it is working. Here is the result:

$ tail /var/log/trackusers.log
07/23/14 12:53: users=(unknown) gboyd:  12:53:24 up  2:40,  2 users,  load average: 0.00, 0.01, 0.05
07/23/14 12:58: users=(unknown) gboyd:  12:58:24 up  2:45,  2 users,  load average: 0.04, 0.04, 0.05
07/23/14 13:03: users=(unknown) gboyd:  13:03:24 up  2:50,  2 users,  load average: 0.09, 0.08, 0.05

and here is its status

$ systemctl status trackusers.service
trackusers.service - Simple system monitoring service
   Loaded: loaded (/etc/systemd/system/trackusers.service; enabled)
   Active: activating (auto-restart) since Wed 2014-07-23 13:08:24 PDT; 1min 29s ago
  Process: 9546 ExecStart=/usr/local/bin/somecmds (code=exited, status=0/SUCCESS)
 Main PID: 9546 (code=exited, status=0/SUCCESS)


Prev This page was made entirely with free software on linux:  
Kompozer, the Mozilla Project
and Openoffice.org      
Next

Copyright 2014 Greg Boyd - All Rights Reserved.