Cron jobs not running

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
toddbailey

Cron jobs not running

Post by toddbailey »

Hi All,


I'm trying to get a cron job to run (under the root account) that does a shutdown at 23:59 with a 15 minute grace period every day of the week

when I run #crontab -l

I get

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user command
#17 * * * * root cd / && run-parts --report /etc/cron.hourly
#25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
#47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
#52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
44 23 * * * root /sbin/shutdown -P 23:59 "server going down in 15 minutes"
#

sudo ps -aux | grep cron
root 1551 0.0 0.0 21324 912 ? Ss 16:18 0:00 cron
todd 12688 0.0 0.0 13632 952 pts/5 S+ 23:51 0:00 grep --colour=auto cron

what am I doing wrong?
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Habitual

Re: Cron jobs not running

Post by Habitual »

I'm not sure it's "wrong" what you've done, but there is another way. ;)

Make a /root/myshutdown.sh script as root and insert

Code: Select all

#!/bin/bash
/sbin/shutdown -P 23:59 "server going down in 15 minutes"
save it and exit.

Code: Select all

chmod 700 /root/myshutdown.sh
use that in the cron editor for root:

Code: Select all

44 23 * * * root /root/myshutdown.sh
That should work.

Typically, any command that has or accepts multiple arguments or switches should be inside a shell script.
and yours has them with "-P 23:59"

Good luck.
eanfrid

Re: Cron jobs not running

Post by eanfrid »

Either way of launching the poweroff command should work (plain line or bash script call) but ... how do you modify your root cron ? You have to use "crontab -e" to edit a cron and make the changes known to the running daemon. Otherwise, you will have to restart the cron daemon. "ps aux|grep cron" will never show you the list of pending cron jobs, either. Only "crontab -l" will show you the schedule list of the selected user.
toddbailey

Re: Cron jobs not running

Post by toddbailey »

the ps -aux was used to prove that the cron service is running.


I'll try the script approach, but in reality, my method should work.
toddbailey

Re: Cron jobs not running

Post by toddbailey »

Well, Still not working. last night at 11:59 the machine was still running,
I manually executed the script to test and since it was after midnight I would have to wait somethuibng like 11,000 minutes to shutdown.

so here is what I have in place so far.

From the command line and su as root

# crontab -l
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user command
#17 * * * * root cd / && run-parts --report /etc/cron.hourly
#25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
#47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
#52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
44 23 * * * root /root/myshutdown.sh



# cat /root/myshutdown.sh
#!/bin/bash
/sbin/shutdown -P 23:59 "server going down at 23:59"


according to ps axu, cron is running

# ps axu | grep cron
root 1523 0.0 0.0 21324 1052 ? Ss 08:30 0:00 cron
root 5489 0.0 0.0 13632 948 pts/5 S+ 09:28 0:00 grep --colour=auto cron


Any Ideas why the cron jobs aren't running?
lime

Re: Cron jobs not running

Post by lime »

I like the idea of just putting a shutdown command for the coming Sunday in your startup scripts. I haven't studied Mint's startup yet. But that just seems like a plain nice idea.

But cron works too. Totally.

I think it is appropriate to put it in the root user's cron table and not the system /etc/crontab since that may be overwritten on many UNIXES.

However the table format for a user crontab is different you do not put the user (in most cron implementation I know of anyway) and I think that is your error toddbailey. Remove " root" it is implicit since it is root's user cron table.

If you examine the logs it may have filed an error about not finding a command named root or something like that.

When you troubleshoot a cron issue you can vet your syntax by making a test event at a convenient time in a couple of minutes from when you input it. For example,

Code: Select all

dale@cutermaster:~$ crontab -l
no crontab for dale
dale@cutermaster:~$ date
Sun Oct 13 12:19:04 PDT 2013
dale@cutermaster:~$ echo '20 12 * * * echo wow | wall' | crontab
dale@cutermaster:~$ date
Sun Oct 13 12:19:40 PDT 2013
                                                                               
Broadcast Message from dale@cutermas                                           
        (somewhere) at 12:20 ...                                               
                                                                               
wow                                                                            
                                                                               
See also the different manual sections because if you type man cron, you might actually want CRON(5) so type man 5 cron.
Locked

Return to “Scripts & Bash”