Systemd: Difference between revisions

From Bitpost wiki
(Created page with "=== Timers === Use these in place of cron. Each one typically does one task. systemd timer services ---------------------- https://www.certdepot.net/rhel7-use-s...")
 
No edit summary
Line 6: Line 6:
     ----------------------
     ----------------------
     https://www.certdepot.net/rhel7-use-systemd-timers/
     https://www.certdepot.net/rhel7-use-systemd-timers/
 
     create a script to do the work:
     create a script to do the work:
       echo "/usr/sbin/logrotate /etc/logrotate.conf" >/usr/local/sbin/logrotate.sh
       echo "/usr/sbin/logrotate /etc/logrotate.conf" >/usr/local/sbin/logrotate.sh
 
     create a service file:
     create a service file:
       nano /usr/lib/systemd/system/logrotate.service
       nano /usr/lib/systemd/system/logrotate.service
         [Unit]
         [Unit]
         Description=Rotate logs
         Description=Rotate logs
 
         [Service]
         [Service]
         Type=simple
         Type=simple
         ExecStart=/usr/local/sbin/logrotate.sh
         ExecStart=/usr/local/sbin/logrotate.sh
         User=root
         User=root
 
         [Install]
         [Install]
         WantedBy=multi-user.target
         WantedBy=multi-user.target
 
     create a timer file:
     create a timer file:
       nano /usr/lib/systemd/system/logrotate.timer
       nano /usr/lib/systemd/system/logrotate.timer
         [Unit]
         [Unit]
         Description=Rotate logs as needed every night at 2am
         Description=Rotate logs as needed every night at 2am
 
         [Timer]
         [Timer]
         OnCalendar=*-*-* 02:00:00
         OnCalendar=*-*-* 02:00:00
         Unit=logrotate.service
         Unit=logrotate.service
 
         [Install]
         [Install]
         WantedBy=multi-user.target
         WantedBy=multi-user.target
 
     activate on boot:
     activate on boot:
       # NOTE you must enable the service (even though not run directly), plus the timer
       # NOTE you must enable the service (even though not run directly), plus the timer
Line 41: Line 41:
       systemctl enable logrotate.timer
       systemctl enable logrotate.timer
       systemctl start logrotate.timer
       systemctl start logrotate.timer
 
     utils:
     utils:
       systemctl is-enabled ####.timer
       systemctl is-enabled ####.timer

Revision as of 12:20, 21 July 2017

Timers

Use these in place of cron. Each one typically does one task.

   systemd timer services
   ----------------------
   https://www.certdepot.net/rhel7-use-systemd-timers/

   create a script to do the work:
     echo "/usr/sbin/logrotate /etc/logrotate.conf" >/usr/local/sbin/logrotate.sh

   create a service file:
     nano /usr/lib/systemd/system/logrotate.service
       [Unit]
       Description=Rotate logs

       [Service]
       Type=simple
       ExecStart=/usr/local/sbin/logrotate.sh
       User=root

       [Install]
       WantedBy=multi-user.target

   create a timer file:
     nano /usr/lib/systemd/system/logrotate.timer
       [Unit]
       Description=Rotate logs as needed every night at 2am

       [Timer]
       OnCalendar=*-*-* 02:00:00
       Unit=logrotate.service

       [Install]
       WantedBy=multi-user.target

   activate on boot:
     # NOTE you must enable the service (even though not run directly), plus the timer
     # then start the timer
     systemctl enable logrotate       
     systemctl enable logrotate.timer
     systemctl start logrotate.timer

   utils:
     systemctl is-enabled ####.timer
     systemctl is-active ####.timer   # to see if timer is active and enabled
     systemctl start ####      # to run service immediately
     systemctl status ####     # nice status output
     systemctl daemon-reload   # to restart services after config changes
     systemctl list-timers [####*]  # to list timers that start with #####