Ask Your Question
1

cron refuses to shutdown my computer

asked 2017-08-02 04:03:30 -0500

francko gravatar image

hi all

i'm on Fedora 26 and i want to set up a cron job to shutdown my computer automatically at a given time, so i can listen to my cd's while i fall into sleep. i'm desperately trying to set up a cron job for this but it doesn't want to work...

that is what i've done:

  1. opened a terminal and prompt the command

gvim crontab -e

this created a new crontab file in /home/frank/ and opened it with gvim

  1. than i typed the cron command into this open file

30 22 * * * frank shutdown -P

this command should shutdown my machine at half past ten (i tried this command with/without user and with different varies of shutdown (h, k, halt etc.)

  1. opened a terminal and prompt the command

crontab -u frank /home/frank/crontab

to copy the file /home/frank/crontab to /var/spool/cron/frank

  1. check with the command

crontab -l

shows my crontab file and the line 30 22 * * * frank shutdown -P

but nothing happens at half past ten :-(

to double-check if i'm with the right file and if cron is really working i set up a backup every 5 minutes with backintime. the check with the command

crontab -l

now shows my crontab file with the line 30 22 * * * frank shutdown -P plus the command from backintime.

and while backintime is writing backups every 5 minutes (what shows that cron is working) no one shuts down my computer at half past ten :-(

any idea? a se-linux problem? is shutdown not allowed with cron?

thanks in advance frank

edit retag flag offensive close merge delete

4 Answers

Sort by ยป oldest newest most voted
1

answered 2017-08-02 18:05:54 -0500

blorg gravatar image

updated 2017-08-02 20:39:16 -0500

Have you checked to see if the cron job is running at all?

$ sudo journalctl -u crond

Also, I think you can shut down a Linux box when you're logged in to the system locally. The cron job will not be run as you as though you were logged in.

Try putting the job in /etc/cron.d w/o any usernames:

$ sudo cat /etc/cron.d/shutdown 30 22 * * * shutdown -h now

And see if that works.

edit flag offensive delete link more

Comments

Might have to fully-path shutdown:

30 22 * * * /sbin/shutdown -h now

blorg gravatar imageblorg ( 2017-08-02 18:07:41 -0500 )edit

the command $ sudo cat /etc/cron.d/shutdown 30 22 * * * shutdown -h now doesn't work at my system. it tells me unknown command. if i try it without $ it claims an unvalid option -- h

francko gravatar imagefrancko ( 2017-08-03 02:43:42 -0500 )edit

The markdown on this system hosed up the formatting, that should have been multiple lines. In the preview of my comment, it showed it correctly. There should be a newline between shutdown and 30. I was trying to demonstrate running the command sudo cat /etc/cron.d/shutdown. The $ was the shell prompt. The rest of it would be the output.

Maybe a little clearer:

In the directory /etc/cron.d/shutdown, put a file that contains 30 22 * * * /sbin/shutdown -h now

blorg gravatar imageblorg ( 2017-08-04 09:45:51 -0500 )edit
0

answered 2017-08-02 10:57:12 -0500

villykruse gravatar image

Drop your user name in the crontab entry.

30 22 * * *  shutdown -P

The user name field is only used with the file /etc/crontab and not the normal crontab files in /var/spool/cron .

edit flag offensive delete link more

Comments

thanks for the answer, but i already tried this command with/without user and with different varies of shutdown (h, k, halt etc.)

30 22 * * * shutdown -P

30 22 * * * shutdown -h

30 22 * * * shutdown -k

30 22 * * * frank shutdown -P

30 22 * * * frank shutdown -h

30 22 * * * frank shutdown -k

30 22 * * * frank@franks-imac shutdown -P

30 22 * * * frank@franks-imac shutdown -h

30 22 * * * frank@franks-imac shutdown -k

non of these varieties will work :-(

francko gravatar imagefrancko ( 2017-08-02 12:58:24 -0500 )edit
0

answered 2017-08-03 03:53:03 -0500

francko gravatar image

updated 2017-08-03 05:20:59 -0500

now it's working :-)

the winner was:

The cron job will not be run as you as though you were logged in

and

Might have to fully-path shutdown: 30 22 * * * /sbin/shutdown -h now

so i logged in the terminal as root and created a new crontab for root with

crontab -e

there i inserted the line

30 22 * * * /sbin/shutdown -h now

and the system now powers off :-)

NOTE: the command '30 22 * * * shutdown -h now' doesn't work, even as root!!!

thanks all for your help!

greetz Frank

edit flag offensive delete link more
0

answered 2017-08-03 02:16:22 -0500

francko gravatar image

updated 2017-08-03 02:24:51 -0500

the hint with the command was excellent. As i read it as a Linux newbie it could be a SE Linux problem, but no idea what this message means. The funny thing is that Backintime writes its backups every 5 minutes.

Here's the enty from todays journal:


  • Reboot --

Aug 03 07:58:44 franks-imac systemd[1]: Started Command Scheduler.

Aug 03 07:58:44 franks-imac crond[795]: (CRON) INFO (Syslog will be used instead of sendmail.)

Aug 03 07:58:44 franks-imac crond[795]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 4% if used.)

Aug 03 07:58:44 franks-imac crond[795]: ((null)) Unauthorized SELinux context=systemu:systemr:systemcronjobt:s0-s0

Aug 03 07:58:44 franks-imac crond[795]: (root) FAILED (loading cron table)

Aug 03 07:58:45 franks-imac crond[795]: (CRON) INFO (running with inotify support)

Aug 03 08:01:01 franks-imac CROND[2391]: (root) CMD (run-parts /etc/cron.hourly)

Aug 03 08:08:25 franks-imac crond[795]: (CRON) INFO (Shutting down)

Aug 03 08:08:25 franks-imac systemd[1]: Stopping Command Scheduler...

Aug 03 08:08:25 franks-imac systemd[1]: Stopped Command Scheduler.

Aug 03 08:08:25 franks-imac systemd[1]: Started Command Scheduler.

Aug 03 08:08:25 franks-imac crond[2993]: (CRON) INFO (Syslog will be used instead of sendmail.)

Aug 03 08:08:25 franks-imac crond[2993]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 92% if used.)

Aug 03 08:08:25 franks-imac crond[2993]: ((null)) Unauthorized SELinux context=systemu:systemr:systemcronjobt:s0-s

Aug 03 08:08:25 franks-imac crond[2993]: (root) FAILED (loading cron table)

Aug 03 08:08:26 franks-imac crond[2993]: (CRON) INFO (running with inotify support)

Aug 03 08:08:26 franks-imac crond[2993]: (CRON) INFO (@reboot jobs will be run at computer's startup.)

Aug 03 09:01:01 franks-imac CROND[4938]: (root) CMD (run-parts /etc/cron.hourly)

Aug 03 09:04:01 franks-imac crond[2993]: (system) RELOAD (/etc/crontab)

Aug 03 09:04:01 franks-imac crond[2993]: ((null)) Unauthorized SELinux context=systemu:systemr:systemcronjobt:s0-s

Aug 03 09:04:01 franks-imac crond[2993]: (root) FAILED (loading cron table)

Aug 03 09:04:01 franks-imac crond[2993]: (frank) RELOAD (/var/spool/cron/frank)

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-08-02 03:49:53 -0500

Seen: 674 times

Last updated: Aug 03 '17