0

I have multiple Redhat & CentOS 7 servers that are used only during working hours. I am looking into using the systemd-shutdownd service to shut down each machine at 6-30pm on workdays.

Systemd appears to be a cleaner solution than cron jobs.

Google shows that there is a schedule file that this service uses, but I have not been able to find how to implement it.

Also, I'd like a way to stop the auto-powerdown in case I work late on a particular day.

  • Sorry for my first message. I've just putted exact solution. – Yurij Goncharuk Apr 19 '18 at 16:36
  • fwiw systemd-shutdownd is (was) just the backend to the standard shutdown.command. shutdown just lets you schedule one shutdown; it doesn't implement a recurring schedule that will run every day. – sourcejedi Apr 19 '18 at 20:47

1 Answers1

4

CentOS 7 had systemd init system.

systemd has a good feature which is named as timer. Timer is like service and is intended for starting services at specific time. systemd shutdown system by calling systemd-poweroff service. So it's need to write systemd-poweroff.timer:

$ cat /etc/systemd/system/systemd-poweroff.timer
[Unit]
Description=Poweroff every work day
# Call necessary service
Unit=systemd-poweroff.service

[Timer]
# Power off in working days at 23:00
OnCalendar=Mon,Tue,Wed,Thu,Fri *-*-* 23:00:00

[Install]
WantedBy=timers.target

It's need to do systemctl enable systemd-poweroff.timer and systemctl start systemd-poweroff.timer for enable and run timer. After, timer will be started:

$ systemctl list-timers
NEXT                         LEFT          LAST PASSED UNIT                         ACTIVATES
Thu 2018-04-19 19:39:36 MSK  14min left    n/a  n/a    systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
Thu 2018-04-19 23:00:00 MSK  3h 34min left n/a  n/a    systemd-poweroff.timer       systemd-poweroff.service

2 timers listed.

Pass --all to see loaded but inactive timers, too.

If you want to disable timer in particular day then it's possible just in case of ordinary systemd service:

# systemctl stop systemd-poweroff.timer
# systemctl list-timers
NEXT                         LEFT       LAST PASSED UNIT                         ACTIVATES
Thu 2018-04-19 19:39:36 MSK  12min left n/a  n/a    systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service

1 timers listed.
Pass --all to see loaded but inactive timers, too.
sourcejedi
  • 50,249