sbaratheon
Posts: 81
Joined: Sun Sep 30, 2012 10:33 am

Cron

Tue Dec 11, 2012 6:11 pm

I'm trying to use cron but when I type:

sudo crontab -e

It says there is no crontab for root and it's using a temporary one :

no crontab for root - using an empty one
File: /tmp/crontab.nxsbBa/crontab

I save the file using Ctrl and O but it doesn't work.

What am I doing wrong here?

Thanks!

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Cron

Tue Dec 11, 2012 6:17 pm

Why use sudo?
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

sbaratheon
Posts: 81
Joined: Sun Sep 30, 2012 10:33 am

Re: Cron

Tue Dec 11, 2012 6:22 pm

No reason.

I get the same problem with:

crontab -e
File: /tmp/crontab.Mdibr4/crontab

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Cron

Tue Dec 11, 2012 6:40 pm

Well, I didn't say that not using sudo would solve your (non-existent) problem. I just wondered why you were using it. As you've correctly surmised, there is no reason to use it (unless you really do want to edit root's crontab file rather than your own).

As to your non-existent problem, it looks like things are working as they should. You don't have a crontab file (yet), but you want to create one. Once you create it, and save it, everything should be hunky-dory.

Incidnetally, the way the crontab editor works, is to make up a silly filename, edit that file in your $EDITOR, then, on exit from the editor, copy or rename that file to the "right" file where cron expects to find it. This is all working-as-designed.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

sbaratheon
Posts: 81
Joined: Sun Sep 30, 2012 10:33 am

Re: Cron

Tue Dec 11, 2012 7:19 pm

Joe Schmoe wrote:Well, I didn't say that not using sudo would solve your (non-existent) problem. I just wondered why you were using it. As you've correctly surmised, there is no reason to use it (unless you really do want to edit root's crontab file rather than your own).

As to your non-existent problem, it looks like things are working as they should. You don't have a crontab file (yet), but you want to create one. Once you create it, and save it, everything should be hunky-dory.

Incidnetally, the way the crontab editor works, is to make up a silly filename, edit that file in your $EDITOR, then, on exit from the editor, copy or rename that file to the "right" file where cron expects to find it. This is all working-as-designed.
So running :

crontab -e

Adding:

***** /home/pi/tom/crontest.o

and saving should run every minute (cron is started btw)?

It doesn't, unfortunately.

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
Contact: Website

Re: Cron

Tue Dec 11, 2012 7:23 pm

Personally I'd edit the /etc/crontab file and tell it the user....
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

sbaratheon
Posts: 81
Joined: Sun Sep 30, 2012 10:33 am

Re: Cron

Tue Dec 11, 2012 7:37 pm

pluggy wrote:Personally I'd edit the /etc/crontab file and tell it the user....
I edited that with :

***** pi /home/pi/tom/crontest.o

and still nothing

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
Contact: Website

Re: Cron

Tue Dec 11, 2012 7:41 pm

It might need to be the root user. Usually if its unattended, running on a schedule, which user is 'running' it makes little odds.
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

User avatar
jojopi
Posts: 3079
Joined: Tue Oct 11, 2011 8:38 pm

Re: Cron

Tue Dec 11, 2012 7:46 pm

sbaratheon wrote:***** /home/pi/tom/crontest.o
You need spaces between the asterisks.

What type of file is crontest.o, and is it executable? Conventionally, *.o are object files (compiled but not linked) and you can not run them.

The fact that you need to be root to edit /etc/crontab, but not to run "crontab -e", tells you that the latter is preferred.

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
Contact: Website

Re: Cron

Tue Dec 11, 2012 7:58 pm

I don't consider running unattended processes as root an issue. Nobody is logged in, the computer isn't going to mistype anything. Its only giving root permissions to the cron job in hand, so a rogue process isn't going to get to use it for its own ends. Theres a world of difference between running a cron job as root and logging in and running everything as root.
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

sbaratheon
Posts: 81
Joined: Sun Sep 30, 2012 10:33 am

Re: Cron

Tue Dec 11, 2012 8:01 pm

It's just a C++ executable which makes outputs the time in a file.

I've tried running crontab -e
adding * * * * * ./crontest.o

to no avail. What does the command actually need to be?

User avatar
jojopi
Posts: 3079
Joined: Tue Oct 11, 2011 8:38 pm

Re: Cron

Tue Dec 11, 2012 8:15 pm

sbaratheon wrote:* * * * * ./crontest.o
You were better with the full path. The working directory when the command is executed will usually be /home/pi, but it is best not to rely on that.

Does the command behave as you expect when you run it manually?

sbaratheon
Posts: 81
Joined: Sun Sep 30, 2012 10:33 am

Re: Cron

Tue Dec 11, 2012 8:26 pm

only when I run cd to the directory and run "./crontest.o " not with "/home/pi/tom/crontest.o"

User avatar
jojopi
Posts: 3079
Joined: Tue Oct 11, 2011 8:38 pm

Re: Cron

Tue Dec 11, 2012 8:33 pm

sbaratheon wrote:only when I run cd to the directory and run "./crontest.o " not with "/home/pi/tom/crontest.o"
Then obviously it will not work from cron without the cd. So try:

Code: Select all

* * * * * cd /home/pi/tom && ./crontest.o
Or, add to the beginning of the source code:

Code: Select all

chdir("/home/pi/tom");
And recompile. Preferably getting rid of the misleading .o suffix at the same time.

rgh
Posts: 211
Joined: Fri Nov 25, 2011 3:53 pm

Re: Cron

Tue Dec 11, 2012 9:02 pm

Here's an example of it working, if it helps. This is on Raspbian.

First I create a crontab file for user richard:

Code: Select all

[email protected] ~ $ crontab -e
no crontab for richard - using an empty one
crontab: installing new crontab
[email protected] ~ $ 
This is what it contains:

Code: Select all

[email protected] ~ $ sudo cat /var/spool/cron/crontabs/richard
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.FerJS8/crontab installed on Tue Dec 11 20:50:10 2012)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
* * * * * $HOME/crontest.sh

[email protected] ~ $ 
This is what /home/richard/crontest.sh contains:

Code: Select all

[email protected] ~ $ cat crontest.sh 
#!/bin/sh

date > /tmp/log

and it is an executable:

Code: Select all

[email protected] ~ $ ls -l crontest.sh 
-rwxr-xr-x 1 richard richard 28 Dec 11 20:50 crontest.sh
So once a minute the current date is written in to /tmp/log (always assuming that file is writable by user richard).

Finally, you can look in the system logs and see that cron noticed I editted the file and you can see it is running it every minute:

Code: Select all

[email protected] ~ $ sudo tail /var/log/syslog
Dec 11 20:49:07 raspberrypi crontab[4040]: (richard) END EDIT (richard)
Dec 11 20:49:19 raspberrypi crontab[4044]: (richard) BEGIN EDIT (richard)
Dec 11 20:50:10 raspberrypi crontab[4044]: (richard) REPLACE (richard)
Dec 11 20:50:10 raspberrypi crontab[4044]: (richard) END EDIT (richard)
Dec 11 20:51:01 raspberrypi /USR/SBIN/CRON[4052]: (richard) CMD ($HOME/crontest.sh)
Dec 11 20:52:01 raspberrypi /USR/SBIN/CRON[4064]: (richard) CMD ($HOME/crontest.sh)
Dec 11 20:53:01 raspberrypi /USR/SBIN/CRON[4088]: (richard) CMD ($HOME/crontest.sh)
Dec 11 20:54:01 raspberrypi /USR/SBIN/CRON[4092]: (richard) CMD ($HOME/crontest.sh)
Dec 11 20:55:01 raspberrypi /USR/SBIN/CRON[4157]: (richard) CMD ($HOME/crontest.sh)
Dec 11 20:56:01 raspberrypi /USR/SBIN/CRON[4166]: (richard) CMD ($HOME/crontest.sh)
[email protected] ~ $ 

sbaratheon
Posts: 81
Joined: Sun Sep 30, 2012 10:33 am

Re: Cron

Tue Dec 11, 2012 9:53 pm

jojopi wrote:
sbaratheon wrote:only when I run cd to the directory and run "./crontest.o " not with "/home/pi/tom/crontest.o"
Then obviously it will not work from cron without the cd. So try:

Code: Select all

* * * * * cd /home/pi/tom && ./crontest.o
Or, add to the beginning of the source code:

Code: Select all

chdir("/home/pi/tom");
And recompile. Preferably getting rid of the misleading .o suffix at the same time.
Ok "cd /home/pi/tom && ./crontest.o" works but " * * * * * root cd /home/pi/tom && ./crontest.o" in etc/crontab doesn't work.

sbaratheon
Posts: 81
Joined: Sun Sep 30, 2012 10:33 am

Re: Cron

Tue Dec 11, 2012 9:55 pm

rgh wrote:Here's an example of it working, if it helps. This is on Raspbian.

First I create a crontab file for user richard:

Code: Select all

[email protected] ~ $ crontab -e
no crontab for richard - using an empty one
crontab: installing new crontab
[email protected] ~ $ 
This is what it contains:

Code: Select all

[email protected] ~ $ sudo cat /var/spool/cron/crontabs/richard
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.FerJS8/crontab installed on Tue Dec 11 20:50:10 2012)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
* * * * * $HOME/crontest.sh

[email protected] ~ $ 
This is what /home/richard/crontest.sh contains:

Code: Select all

[email protected] ~ $ cat crontest.sh 
#!/bin/sh

date > /tmp/log

and it is an executable:

Code: Select all

[email protected] ~ $ ls -l crontest.sh 
-rwxr-xr-x 1 richard richard 28 Dec 11 20:50 crontest.sh
So once a minute the current date is written in to /tmp/log (always assuming that file is writable by user richard).

Finally, you can look in the system logs and see that cron noticed I editted the file and you can see it is running it every minute:

Code: Select all

[email protected] ~ $ sudo tail /var/log/syslog
Dec 11 20:49:07 raspberrypi crontab[4040]: (richard) END EDIT (richard)
Dec 11 20:49:19 raspberrypi crontab[4044]: (richard) BEGIN EDIT (richard)
Dec 11 20:50:10 raspberrypi crontab[4044]: (richard) REPLACE (richard)
Dec 11 20:50:10 raspberrypi crontab[4044]: (richard) END EDIT (richard)
Dec 11 20:51:01 raspberrypi /USR/SBIN/CRON[4052]: (richard) CMD ($HOME/crontest.sh)
Dec 11 20:52:01 raspberrypi /USR/SBIN/CRON[4064]: (richard) CMD ($HOME/crontest.sh)
Dec 11 20:53:01 raspberrypi /USR/SBIN/CRON[4088]: (richard) CMD ($HOME/crontest.sh)
Dec 11 20:54:01 raspberrypi /USR/SBIN/CRON[4092]: (richard) CMD ($HOME/crontest.sh)
Dec 11 20:55:01 raspberrypi /USR/SBIN/CRON[4157]: (richard) CMD ($HOME/crontest.sh)
Dec 11 20:56:01 raspberrypi /USR/SBIN/CRON[4166]: (richard) CMD ($HOME/crontest.sh)
[email protected] ~ $ 
Thanks for that. When I run crontab -e I get a file opened with instructions commented out. I simply add the cron command to the bottom and save. I don't get any message about "installing crontab"? When I run crontab -l afterwards I just get "crontab for pi".

rgh
Posts: 211
Joined: Fri Nov 25, 2011 3:53 pm

Re: Cron

Tue Dec 11, 2012 10:21 pm

Sounds like you haven't actually saved the crontab. I don't know what editor you are using. I did this to use my favourite editor:

Code: Select all

$ export EDITOR=vi
$ crontab -e
Maybe some combination of your editor and the way crontab -e works with a tmp file caused a problem. Try a different editor, perhaps.

efflandt
Posts: 359
Joined: Mon Dec 03, 2012 2:47 am
Location: Elgin, IL USA

Re: Cron

Wed Dec 12, 2012 5:22 am

Perhaps a silly question, but who owns the executable and what permissions does it have for whom (ls -l), since it is in /home/pi instead of in /root? Maybe root does not have permission to execute it, or if permissions are too liberal, maybe root does not trust it.

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Cron

Wed Dec 12, 2012 9:28 am

The bottom line here is:

1) There are lots of thngs that can go wrong when setting things up to run under cron - and if you're not familiar with it, then it will be a learning experience.

2) If you really do want something to run every minute, then I suggest that cron isn't the right vehicle for that. For a variety of reasons, running something every minute under cron can cause all sorts of issues, particularly during the development stages (i.e., when you don't have your program fully debugged). Instead, I suggest having the script contain its own "do it every minute" logic, like this:

while sleep 60;do
...
done

Now, the script just has to be launched once. And, finally, you should run it in a virtual console, not via cron. This can be done by modifying /etc/inittab to run this script on a VC (usually *not* tty1, BTW). Details will be provided if necessary.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

sbaratheon
Posts: 81
Joined: Sun Sep 30, 2012 10:33 am

Re: Cron

Wed Dec 12, 2012 5:30 pm

Ah, got it working!

Thanks for everyone's help!

User avatar
Dweeber
Posts: 606
Joined: Fri Aug 17, 2012 3:35 am
Location: Mesa, AZ
Contact: Website

Re: Cron

Wed Dec 12, 2012 5:49 pm

If you are looking at your user account crons, you should never have to do such contortions like:

[email protected] ~ $ sudo cat /var/spool/cron/crontabs/richard

There is a command that lets you look at your user crontabs using... so if you are logged in as richard....

[email protected] ~ $ crontab -l

I also never use crontab -e .... prone to problems especially if you are using many different systems. if your editor settings are messed up or not right, you could end up with a blank crontab using crontab -e.

Instead, I teach my users to use a "process" of dealing with crontabs...

First get your current crontab entries:

$ crontab -l > cron.current

Then edit the file with your favorite editor....

Next save the changes using:

$ crontab cron.current

And ALWAYS check what you have with...

$ crontab -l

to make sure you did what you think you did.

This can also be used to keep copies of your crontab entries so that if you make a mistake, you can go back and see what you had. it is common to make a change to your crontab many days, months, years later....

Save an archive of what was there and a version you can edit...

$ crontab -l > cron.2012-12-11
$ crontab -l > cron.current

edit

$ crontab cron.current
$ crontab -l

etc...
Dweeber A.K.A. Kevin...
My RPI Info Pages including Current Setup - http://rpi.tnet.com

Return to “Beginners”