Cron jobs not running

Forum rules
Before you post please read this

Cron jobs not running

Postby toddbailey on Thu Sep 19, 2013 2:53 am

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?
toddbailey
Level 1
Level 1
 
Posts: 44
Joined: Sat Mar 31, 2012 3:59 pm

Linux Mint is funded by ads and donations.
 

Re: Cron jobs not running

Postby Habitual on Thu Sep 19, 2013 9:32 am

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.
What's a landing but a take off in reverse?
My DorkBlog
User avatar
Habitual
Level 8
Level 8
 
Posts: 2213
Joined: Sun Nov 21, 2010 8:31 pm
Location: LM17Q-Xfce

Re: Cron jobs not running

Postby eanfrid on Thu Sep 19, 2013 11:13 am

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.
Main desktop: Debian GNU/Linux Wheezy 64bit - MATE 1.8.1
(i5 2400@3.7GHz - 16GB DDR3 - HD6770 w/radeon driver - SSD+RAID1)
Safer than Dropbox
User avatar
eanfrid
Level 7
Level 7
 
Posts: 1871
Joined: Mon Apr 30, 2012 2:49 am
Location: FR

Re: Cron jobs not running

Postby toddbailey on Thu Sep 19, 2013 4:45 pm

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
Level 1
Level 1
 
Posts: 44
Joined: Sat Mar 31, 2012 3:59 pm

Re: Cron jobs not running

Postby toddbailey on Fri Sep 20, 2013 12:29 pm

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?
toddbailey
Level 1
Level 1
 
Posts: 44
Joined: Sat Mar 31, 2012 3:59 pm

Re: Cron jobs not running

Postby lime on Sun Oct 13, 2013 3:20 pm

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.
lime
Level 1
Level 1
 
Posts: 8
Joined: Sat Sep 28, 2013 6:55 am
Location: Shoreline, WA


Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 0 guests