LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   cron job not running (https://www.linuxquestions.org/questions/linux-software-2/cron-job-not-running-215844/)

vincebs 08-10-2004 12:05 PM

cron job not running
 
Hi everyone,

I want my computer to shut itself down at 1am everyday. So what I did was type "su -c 'crontab -e'" and added in the line:

Code:

0 1 * * * init 0
Then I restarted crond by typing "/etc/init.d/crond restart". To test it, I reset my clock to 12:55am and rebooted. But when the hour rolled over, nothing happened.

I tried different things like adding quotation marks to "init 0", putting the full path ("/sbin/init 0"), and using an alternative command ("shutdown -h now"), but to no avail.

What am I doing wrong?

Thanks,

Vince

david_ross 08-10-2004 12:45 PM

Try creating a log file to test:
0 1 * * * init 0 2>&1 > /tmp/shutdown.log

Do you get anything in the log when it is supposed to run?

vincebs 08-13-2004 03:54 PM

No, apparently it doesn't even run because when I type "cat /tmp/shutdown.log", the file hasn't even been created.

michaelk 08-13-2004 04:17 PM

Have you tried
/sbin/halt
or
/sbin/poweroff
Instead of init 0

david_ross 08-14-2004 06:29 AM

If that doesn't work it would be interesting to see if your cron jobs are even being run - try this one for example:
0 1 * * * date 2>&1 > /tmp/cron-test.log

Do you see the correct date and time in the log file? ie - 1:30

wmartino 08-14-2004 11:12 AM

You might try creating an empty log file first. Sometomes it will not create the file for you. Try this and see if it works.

Code:

touch /tmp/cron-test.log > /dev/null

vincebs 08-23-2004 10:32 PM

So I added the line "29 23 * * * date 2>&1 > /tmp/shutdown.log" to my crontab file, restarted crond, and made sure that I typed "touch shutdown.log" before 23:29. But yet nothing shows up in /tmp/shutdown.log.

Any more ideas?

Thanks,
Vince

P.S. Hurray, my 400th post!

david_ross 08-24-2004 08:24 AM

What procedure did you use to add it to your crontab file?

vincebs 08-24-2004 01:48 PM

su
crontab -e
<edit>
crontab -l (to verify)
/etc/init.d/crond restart

vincebs 09-01-2004 12:23 PM

bump

nitin_batta 09-01-2004 12:35 PM

Hey just a simple test to verify whether your crond is runninig

do

<code>#su -
#touch /root/cron_test
#crontab -e

and add the following job :

0,5,10,15,20,25,30,35,40,45,50,55 * * * * date >> /root/cron_test</code>

After saving the file just verify crontab -l and also there is no need restart crond it will rebuilt its files again.

This is only a test script and will put the output of date command in /root/cron_test after 5 mins every hour.

Remember to remove it once you have tested it.



.... Nitin Batta ....
http://nitinb.blogspot.com

__________________
If this helped you, please take the time to rate the value of this post by clicking the affero button

sjspig 09-03-2004 01:18 AM

Not sure if this is it, but make sure that the init file is in the path that cron is using to run. It does not run with the full path that your user account runs as. It also runs as the user for which the cron job was created - you will likely have to be root to run this cron job as it is written.

Put the full path to init - probably /sbin/init or /usr/sbin/init - It is common that the path that cron uses is very minimal unless you specify path as a variable in cron:

PATH = /usr/sbin:/sbin:/bin:/usr/bin.....etc...

vincebs 09-06-2004 12:49 PM

Quote:

Originally posted by nitin_batta
Hey just a simple test to verify whether your crond is runninig

do

<code>#su -
#touch /root/cron_test
#crontab -e

and add the following job :

0,5,10,15,20,25,30,35,40,45,50,55 * * * * date >> /root/cron_test</code>

After saving the file just verify crontab -l and also there is no need restart crond it will rebuilt its files again.

This is only a test script and will put the output of date command in /root/cron_test after 5 mins every hour.

Looks like cron works here. But I can't figure out why it won't load "init 0"

Quote:

Originally posted by sjspig
Put the full path to init - probably /sbin/init or /usr/sbin/init - It is common that the path that cron uses is very minimal unless you specify path as a variable in cron
Answer:
Quote:

Originally posted by vincebs
I tried different things like adding quotation marks to "init 0", putting the full path ("/sbin/init 0"), and using an alternative command ("shutdown -h now"), but to no avail.
So I have to be root in order for the cron job to run? I don't feel comfortable leaving my Internet-connected computer running as root all the time...

There's no other way to schedule a shutdown?

nitin_batta 09-06-2004 01:07 PM

Shutdown has execute permissions generally for only for the root.

Quote:

So I have to be root in order for the cron job to run? I don't feel comfortable leaving my Internet-connected computer running as root all the time...

So you will set the shutdown as a cronjob for root.

When you set a cronjob as any user you need not be logged in as that user for the job to run .... so its to set shutdown as a cronjob for root.

#which shutdown
/usr/bin/shutdown (Dummy O/p)

so u'r crontab would be

#crontab -e
0 1 * * * /usr/bin/shutdown -h now

vincebs 09-07-2004 04:43 PM

That's what I did originally but it didn't work.

I made sure that I su'ed before typing crontab -e. But nothing seems to happen unless I'm logged in as root.

nitin_batta 09-07-2004 11:22 PM

Quote:

I made sure that I su'ed before typing crontab -e. But nothing seems to happen unless I'm logged in as root.
You mean to say you did :

Code:

$su -
Password : xxxx
#crontab -e

That is the way to do it and then log off.

Crontab will execute all commands under the root privilidges.

Also you can look at the logs in /var/log/messages to check it cron throws an error while running it.

vincebs 09-09-2004 02:47 PM

Quote:

Originally posted by nitin_batta
You mean to say you did :

Code:

$su -
Password : xxxx
#crontab -e

That is the way to do it and then log off.

Crontab will execute all commands under the root privilidges.

Yep. To be more precise, this is what I did:
1.) I typed "su"
2.) I entered the password.
3.) I typed "crontab -e".
4.) I added the line to shut the computer down.
5.) I saved.
6.) I typed "crontab -l" to verify. Everything okay.
7.) I typed "exit".

But nothing happened when it was supposed to shut down.

Quote:

Originally posted by nitin_batta
Also you can look at the logs in /var/log/messages to check it cron throws an error while running it.
I did a "cat /var/log/messages | grep cron " (under root of course) and nothing showed up except for "crond startup/shutdown succeeded" messages. Nothing to do with my particular cronjob.

vincebs 09-25-2004 10:13 PM

bump

I even decided to use KCron to add the command graphically to my crontab but when 1am rolls around, nothing happens.

Anyone?

sjspig 09-27-2004 10:21 AM

Does the user the cron is running as have rights to shutdown the computer?

Worstje 09-27-2004 01:17 PM

Try using "su -", including the dash "-".

vincebs 09-27-2004 05:22 PM

Where and when do I type "su -"? It seems to do the same thing as "su".

What is "su -" supposed to do.

sjspig: The command should run as root even if the computer is logged on as a normal user.

nitin_batta 09-28-2004 12:24 AM

Quote:

Where and when do I type "su -"? It seems to do the same thing as "su".

What is "su -" supposed to do.
su will give you root privilidges but will not change and the home dir to root and also will not change environment varibles like PATH etc.

su - will also give you root priv along with setting home dir to root and also will also change environment varibles like PATH acc to the specified in the initialisation file (eg .bash_profile or .bashrc) etc.
:Pengy:

sjspig 09-28-2004 01:38 PM

I think my reply was not worded correctly. Does the user for which the cronjob exists currently have rights to shutdown the box? In other words, can you shutdown the box logged in as the user that you are defining the cron job for?

vincebs 09-28-2004 07:04 PM

Quote:

Originally posted by sjspig
I think my reply was not worded correctly. Does the user for which the cronjob exists currently have rights to shutdown the box? In other words, can you shutdown the box logged in as the user that you are defining the cron job for?
Hmm I assumed it doesn't matter, since it's not the user who's running the commands in root's crontab.

nitin_batta: I think I get it. But where would I use "su -"? I can't put it in crontab because it would ask for a password.

sjspig 09-28-2004 07:25 PM

You might use sudo instead - that way you could set up the exact command and let that user run that command when the cronjob is executed. Something like:

0 2 * * * /usr/sbin/sudo /usr/sbin/shutdown -h now

The user running the cronjob must have rights normally to shutdown the server. If you run /usr/sbin/shutdown -h now and the user can't shut the machine off then the user's crontab won't be able to either. There are other ways to make this work but the bottom line is that the user must have rights or it can't be done via crontab. Can you explain the user situation and if all of this sounds correct?

vincebs 09-30-2004 02:46 PM

Quote:

Originally posted by sjspig
You might use sudo instead - that way you could set up the exact command and let that user run that command when the cronjob is executed. Something like:

0 2 * * * /usr/sbin/sudo /usr/sbin/shutdown -h now

The user running the cronjob must have rights normally to shutdown the server. If you run /usr/sbin/shutdown -h now and the user can't shut the machine off then the user's crontab won't be able to either. There are other ways to make this work but the bottom line is that the user must have rights or it can't be done via crontab. Can you explain the user situation and if all of this sounds correct?

But even when I'm a normal user, I can shutdown the computer from the GNOME desktop. However, "init 0" and "shutdown" don't work unless I am root. Is there a way I can get cron to run the GNOME shutdown command, which does not require root privileges?

sjspig 09-30-2004 07:05 PM

I don't know the exact process the gnome desktop uses to shutdown, but I imagine that is does something funky behind the scenes to make this work for you. Your best bet to do this command line is to use sudo and have it run the specific command in sudo. You could also just modify the crontab for root and have root shutdown when you want instead.

vincebs 10-01-2004 03:04 PM

Quote:

Originally posted by sjspig
I don't know the exact process the gnome desktop uses to shutdown, but I imagine that is does something funky behind the scenes to make this work for you. Your best bet to do this command line is to use sudo and have it run the specific command in sudo. You could also just modify the crontab for root and have root shutdown when you want instead.
All the crontab modifications I've mentioned so far were to root's crontab. I su, then I do crontab -e, save, exit. I am wondering if root's crontab will only run if I am logged in as root.

nitin_batta 10-03-2004 11:39 PM

Hi can you post your crontab entry that you are trying .
Acc to me whena cronjob is run the command does not get executed with the rights of crontab userr it gets executed under the priv of the user (root in your case) under whose name the crontab is set.

To just do a check you can try and set the suid bit on the shutdown binary.

Hope this shld work as now any user can shutdown the system from cli

:Pengy:

vincebs 10-09-2004 01:40 AM

How do I change the suid on a binary?

The crontab entry is 0 1 * * * init 0

I have also tried

0 1 * * * /sbin/init 0
0 1 * * * "/sbin/init 0" (so the path is not the problem)
0 1 * * * /sbin/shutdown -h now
0 1 * * * "/sbin/shutdown -h now" (so using a diff. command is not the problem)

None have worked.

nitin_batta 10-09-2004 03:49 AM

Quote:

How do I change the suid on a binary?

The crontab entry is 0 1 * * * init 0

I have also tried

0 1 * * * /sbin/init 0
0 1 * * * "/sbin/init 0" (so the path is not the problem)
0 1 * * * /sbin/shutdown -h now
0 1 * * * "/sbin/shutdown -h now" (so using a diff. command is not the problem)

None have worked.

You can set the suid bit using the chmod command.

Code:

# chmod 4666 /sbin/shutdown
A 4 here sets the suid bit. I dunn have a linux system with me as of now but man chmod:study: could help.

vincebs 10-30-2004 12:35 AM

Hmm something's bizarre... I did chmod 555 to shutdown so now I can shutdown without being root

but the cron job still doesn't load. This time I decided to use KCron to schedule my job. Here's my output for crontab -l:

# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/kde-vicente/kcronCPW9Ja.tmp installed on Fri Oct 29 15:32:53 2004)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
# apagación
0,15,30,45 0,1,2,3 * * 1,2,3,4,5 shutdown -h now
# apagación weekend
30 1,2,3,4,5 * * 6,7 shutdown -h now
# This file was written by KCron. Copyright (c) 1999, Gary Meyer
# Although KCron supports most crontab formats, use care when editing.
# Note: Lines beginning with "#\" indicates a disabled task.

It's 1:34am on Saturday and the scheduled 1:30 shutdown still hasn't come.

nitin_batta 10-30-2004 02:12 AM

Dude can you make set the uid bit on shutdown

assuming you have the binary shutdown at /sbin/shutdown

Code:

now become root and

#chmod 4555 /sbin/shutdown

Now put it normal crontab and check.
Only one issue here is any user on the machine can shut it down.

Nitin :)

cbe 10-30-2004 04:12 AM

Have you tried this:

Edit as root: /etc/crontab

0 1 * * * root /sbin/shutdown -h now

This will execute the command as root (assuming cron is working correctly) w/out sudo.

---

If you were want to use SUDO you have to edit the sudoers file and add a NOPASSWD:shutdown to that user to allow it to shutdown without a password.

example susoers file:

User_Alias SHUTDWN = user
Cmnd_Alias SHUTDOWN = /sbin/shutdown
SHUTDWN = ALL=NOPASSWD:SHUTDOWN

Then you could use crontab as user with SUDO to shutdown the machine w/out a password.


Hopfully this helps in some way.

vincebs 10-30-2004 01:27 PM

Thanks guys, it works now. The main problem was with the permissions to run shutdown and putting that command's path in.

:)


All times are GMT -5. The time now is 03:32 AM.