crontab

I have written a shell script which I run daily and which I now like to run automatically. It requires root privileges - I am the only user and root.

I clearly understand how to set up the 5 time parameters. But I am confused as to where to put that crontab file, what to call it and where to store it (I use the vi editor) - /bin, /sbin or /home/pe1800(me)/bin? I thought that perhaps those crontab files already exist, does not the locate database get automatically updated daily? I tried to find those crontab files but got nothing:

pe1800@linux-gkd7:~> pwd
/home/pe1800
pe1800@linux-gkd7:~> crontab -l
no crontab for pe1800
pe1800@linux-gkd7:~> su
Password:
linux-gkd7:/home/pe1800 # cd /root
linux-gkd7:~ # crontab -l
no crontab for root
linux-gkd7:~ # exit
exit
pe1800@linux-gkd7:~>

Could you perhaps give me some pointers. Thank you,
pe1800

As the root user, you should be able to do:


# crontab -u root

This should put you into an editor (defaults to “vi”, but environment EDITOR should be able to change that). You edit it as you want (such as reading in the lines you have saved elsewhere).

When you exit from the editor, the crontab should be saved wherever it is supposed to go.

This should also send a signal to the running “cron” to tell it to rescan its crontabs.

On 2013-05-28 02:46, nrickert wrote:

> As the root user, you should be able to do:
>
> Code:
> --------------------
>
> # crontab -u root
>
> --------------------

Sure? I tried and it doesn’t work:



cer@Telcontar:~> crontab -u cer
must be privileged to use -u
cer@Telcontar:~>

....

Telcontar:~ # crontab -u root
crontab: usage error: file name must be specified for replace
usage:  crontab -u user] file
crontab -u user]  -e | -l | -r ]
crontab -n  hostname ]
crontab -c
(default operation is replace, per 1003.2)
-e      (edit user's crontab)
-l      (list user's crontab)
-r      (delete user's crontab)
-i      (prompt before deleting user's crontab)
-n      (set host in cluster to run users' crontabs)
-c      (get host in cluster to run users' crontabs)
-s      (selinux context)
Telcontar:~ #

To edit you need “-e”. The “-u” is to choose a user, and it defaults to
the current one. So:


# crontab -e

should do. :slight_smile:

The other place to do it, is simply edit the system crontab in
“/etc/crontab”, or add a file to “/etc/cron.d/”, which has a slightly
different syntax.


Cheers / Saludos,

Carlos E. R.
(from 12.1 x86_64 “Asparagus” at Telcontar)

On 05/27/2013 09:13 PM, Carlos E. R. pecked at the keyboard and wrote:
> On 2013-05-28 02:46, nrickert wrote:

How about this part of the request:

>> As the root user, you should be able to do:

>>
>> Code:
>> --------------------
>>
>> # crontab -u root
>>
>> --------------------

Should be:



# crontab -u root -e


or as the prompt indicates that root is the user simply use:



# crontab -e


Sure? I tried and it doesn’t work:

Because you are using the command as root.


>
> cer@Telcontar:~> crontab -u cer
> must be privileged to use -u
> cer@Telcontar:~>
>
> ....
>
> Telcontar:~ # crontab -u root
> crontab: usage error: file name must be specified for replace
> usage:  crontab -u user] file
>          crontab -u user]  -e | -l | -r ]
>          crontab -n  hostname ]
>          crontab -c
>                  (default operation is replace, per 1003.2)
>          -e      (edit user's crontab)
>          -l      (list user's crontab)
>          -r      (delete user's crontab)
>          -i      (prompt before deleting user's crontab)
>          -n      (set host in cluster to run users' crontabs)
>          -c      (get host in cluster to run users' crontabs)
>          -s      (selinux context)
> Telcontar:~ #
> 

To edit you need “-e”. The “-u” is to choose a user, and it defaults to
the current one. So:


> # crontab -e
> 

>
> should do. :slight_smile:
>
>
> The other place to do it, is simply edit the system crontab in
> “/etc/crontab”, or add a file to “/etc/cron.d/”, which has a slightly
> different syntax.

It is recommended that crontab -e is always used.

On 2013-05-28 03:38, Ken Schneider wrote:
> On 05/27/2013 09:13 PM, Carlos E. R. pecked at the keyboard and wrote:

>> Sure? I tried and it doesn’t work:
>
> Because you are using the command as root.

I tried both as root and as normal user. See the post again, please.

>> The other place to do it, is simply edit the system crontab in
>> “/etc/crontab”, or add a file to “/etc/cron.d/”, which has a slightly
>> different syntax.
>
> It is recommended that crontab -e is always used.

It is recommended for the root /user/, but you can do it elsewhere, as
system jobs.


Cheers / Saludos,

Carlos E. R.
(from 12.1 x86_64 “Asparagus” at Telcontar)

On 05/27/2013 10:38 PM, Carlos E. R. pecked at the keyboard and wrote:
> On 2013-05-28 03:38, Ken Schneider wrote:
>> On 05/27/2013 09:13 PM, Carlos E. R. pecked at the keyboard and wrote:
>
>>> Sure? I tried and it doesn’t work:
>> Because you are using the command as root.
> I tried both as root and as normal user. See the post again, please.
>

As root user:



hp9925:~ # crontab -e
no crontab for root - using an empty one
crontab: no changes made to crontab
hp9925:~ # crontab -u ken -e
no crontab for ken - using an empty one
crontab: no changes made to crontab
hp9925:~ #

/code

As normal user:


ken@hp9925:~> crontab -e
no crontab for ken - using an empty one
crontab: no changes made to crontab
ken@hp9925:~>



Perhaps you have a problem with your system. And only root allows the -u
option apparently.

Ken

On 2013-05-28 04:59, Ken Schneider wrote:
> On 05/27/2013 10:38 PM, Carlos E. R. pecked at the keyboard and wrote:

> Perhaps you have a problem with your system. And only root allows the -u
> option apparently.

You are not reading.

nrickert said that we should do “crontab -u root” to edit the crontab,
and I said “no”, and proved it by trying and showing the error that
happens, doing it both as root and as plain user. I said that the
correct method is doing “crontab -e”.

I know very well that “crontab -u” only runs as root, because it is the
only user that can choose which crontab to work with. But the “-u”
option alone does nothing, you have to use some other option with it (l,
r, e, etc).


Cheers / Saludos,

Carlos E. R.
(from 12.1 x86_64 “Asparagus” at Telcontar)

On 05/27/2013 10:59 PM, Ken Schneider pecked at the keyboard and wrote:
> On 05/27/2013 10:38 PM, Carlos E. R. pecked at the keyboard and wrote:
>> On 2013-05-28 03:38, Ken Schneider wrote:
>>> On 05/27/2013 09:13 PM, Carlos E. R. pecked at the keyboard and wrote:
>>
>>>> Sure? I tried and it doesn’t work:
>>> Because you are using the command as root.
>> I tried both as root and as normal user. See the post again, please.
>>
>
> As root user:
>
>


>
> hp9925:~ # crontab -e
> no crontab for root - using an empty one
> crontab: no changes made to crontab
> hp9925:~ # crontab -u ken -e
> no crontab for ken - using an empty one
> crontab: no changes made to crontab
> hp9925:~ #
>
> [/code
>
> As normal user:
>
> 

ken@hp9925:~> crontab -e
no crontab for ken - using an empty one
crontab: no changes made to crontab
ken@hp9925:~>


>
> Perhaps you have a problem with your system. And only root allows the
> -u option apparently.
>
> Ken[/color]
Meant to add the following under normal user:


ken@hp9925:~> crontab -u root -e
must be privileged to use -u
ken@hp9925:~>



Which clearly states -u option is for root only.

Ken

I’m not sure why this got to an argument between Ken and Carlos.

I took Carlos to be pointing out (correctly) that I forgot the “-e” in my comment.

And sure, Carlos was unnecessarily verbose. But it should not have led the argument that followed, especially since both participants in the argument seem to agree on the main point.

On 2013-05-28 05:46, nrickert wrote:
>
> I’m not sure why this got to an argument between Ken and Carlos.
>
> I took Carlos to be pointing out (correctly) that I forgot the “-e” in
> my comment.
>
> And sure, Carlos was unnecessarily verbose. But it should not have led
> the argument that followed, especially since both participants in the
> argument seem to agree on the main point.

Accepted.


Cheers / Saludos,

Carlos E. R.
(from 12.1 x86_64 “Asparagus” at Telcontar)

On 05/27/2013 11:46 PM, nrickert pecked at the keyboard and wrote:
> I’m not sure why this got to an argument between Ken and Carlos.
>
> I took Carlos to be pointing out (correctly) that I forgot the “-e” in
> my comment.
>
> And sure, Carlos was unnecessarily verbose. But it should not have led
> the argument that followed, especially since both participants in the
> argument seem to agree on the main point.
>
>

Yep, my bad. I misinterpreted what Carlos had presented. My take on his
comments was that it wasn’t working on his system as well and didn’t
understand why. Apologies to Carlos. :slight_smile:

On 2013-05-28 16:53, Ken Schneider wrote:
> On 05/27/2013 11:46 PM, nrickert pecked at the keyboard and wrote:
>> I’m not sure why this got to an argument between Ken and Carlos.
>>
>> I took Carlos to be pointing out (correctly) that I forgot the “-e” in
>> my comment.
>>
>> And sure, Carlos was unnecessarily verbose. But it should not have led
>> the argument that followed, especially since both participants in the
>> argument seem to agree on the main point.
>>
>>
>
> Yep, my bad. I misinterpreted what Carlos had presented. My take on his
> comments was that it wasn’t working on his system as well and didn’t
> understand why. Apologies to Carlos. :slight_smile:

No problem :slight_smile:


Cheers / Saludos,

Carlos E. R.
(from 12.1 x86_64 “Asparagus” at Telcontar)

I see I started quite a discussion with my original request for guidance. Thank you, people, it is quite instructive for a newbie like me. I, too, found that crontab -u root does not work. And I saw a warning somewhere in the documentation not to use crontab -e direct but to just use a plain editor to edit, what I understant also from your comments - to be /etc/crontab.

What I did, as root:

I tried to list crontab with crontab -l which did not work
linux-gkd7:/etc # crontab -u root crontab
#** I recreated crontab which I did not really intend to do but then crontab -l worked**
linux-gkd7:/etc # crontab -l
**# DO NOT EDIT THIS FILE - edit the master and reinstall. }

(crontab installed on Tue May 28 10:57:06 2013) } the system added these comments but the actual code did not change and /etc/crontab was not affected at all

(Cronie version 4.2) }**

# original /etc crontab file:

SHELL=/bin/sh
PATH=/usr/bin:/usr/sbin:/sbin:/bin:/usr/lib/news/bin
MAILTO=pe1800

check scripts in cron.hourly, cron.daily, cron.weekly, and cron.monthly

-*/15 * * * * root test -x /usr/lib/cron/run-crons && /usr/lib/cron/run-crons
>/dev/null 2>&1

Should I now edi[size=2]t /etc/crontab with vi, add /home/pe1800([/size]me[size=2])/bin to the PATH[/size], add a line: [size=2]7 11 * * * root [/size]( script needs to be run as root) **/home/pe1800/bin/**avg.sh,
save[size=2] /etc/crontab [size=1]and then[size=2] crontab -u root crontab, to install it to usr/bin?[/size][/size][/size]

Thank for your help, greetings,
pe1800

On 05/28/2013 02:36 PM, pe1800 wrote:
>
> I see I started quite a discussion with my original request for
> guidance. Thank you, people, it is quite instructive for a newbie like
> me. I, too, found that crontab -u root does not work. And I saw a
> warning somewhere in the documentation not to use crontab -e direct but
> to just use a plain editor to edit, what I understant also from your
> comments - to be /etc/crontab.
>
> What I did, as root:
>
> I tried to list crontab with crontab -l which did not
> work

> linux-gkd7:/etc # crontab -u root crontab
> #* I recreated crontab which I did not really intend to do but
> then crontab -l worked*
> linux-gkd7:/etc # crontab -l
> # DO NOT EDIT THIS FILE - edit the master and reinstall. }
> # (crontab installed on Tue May 28 10:57:06 2013)
> } the system added these comments but the actual code did not change and
> /etc/crontab was not affected at all
> # (Cronie version 4.2)
>
> }

> #
> # ORIGINAL /ETC CRONTAB FILE:
> #
> SHELL=/BIN/SH
> PATH=/USR/BIN:/USR/SBIN:/SBIN:/BIN:/USR/LIB/NEWS/BIN
> MAILTO=PE1800
> #
> # CHECK SCRIPTS IN CRON.HOURLY, CRON.DAILY, CRON.WEEKLY, AND
> CRON.MONTHLY
> #
> -*/15 * * * * ROOT TEST -X /USR/LIB/CRON/RUN-CRONS &&
> /USR/LIB/CRON/RUN-CRONS
>> /DEV/NULL 2>&1
>
> SHOULD I NOW EDIT /ETC/CRONTAB
> WITH VI, ADD
> /HOME/PE1800(ME)/BIN
> TO THE PATH
, *ADD A LINE: 7 11 *
> * * ROOT *( SCRIPT NEEDS TO BE RUN AS ROOT)
> */HOME/PE1800/BIN/*AVG.SH,
> SAVE /ETC/CRONTAB AND THEN CRONTAB
> -U ROOT CRONTAB, TO INSTALL IT TO USR/BIN?
> Thank for your help, greetings,
> pe1800

Do NOT mess with the files in /etc.

If you want to change root’s crontab, then “sudo crontab -e”.

To change your user crontab, then use ‘crontab -e’.

To change crontab for some other user, log in as that user.

Thank you. And after I edit with sudo crontab -e and save, it will automatically update the actual crontab in both /etc and in /usr/bin ?

No. It saves your edit in /var/spool/cron/tabs/username.
The crontab in /etc/ is not changed. And there is no crontab in /usr/bin/, only the command “crontab” is in there.

Thank you. I did a sudo crontab -e, added /home/pe1800/bin - for my script - to PATH, and 7 11 * * * root /home/pe1800/bin/avg.sh, saved the vi edit. Then did a sudo crontab -l and it showed my changes, again with a warning not to edit that file directly. I did a /var/spool/cron/tabs # cat root and everything is there. I also did an /etc cat crontab and, as you said, it hasn’t changed. That’s a mystery to me but, after your warning, I didn’t touch it. Will it, perhaps, change after rebooting?

But what troubles me is when checking the mail it said I had - under MY userid - there was a message: /bin/sh: root: command not found. SHELL=/bin/sh was and still is the very first line in crontab, and I never touched it! How come?

Thank you again and cheers,
pe

On 2013-05-29 02:16, pe1800 wrote:
>
> Thank you. I did a sudo crontab -e, added /home/pe1800/bin - for my
> script - to PATH, and 7 11 * * * root /home/pe1800/bin/avg.sh, saved
> the vi edit. Then did a sudo crontab -l and it showed my changes, again
> with a warning not to edit that file directly.

Please, the commands you post are not readable. Use code tags for
printouts and commands. Advanced editor, ‘#’ button.
Posting in
Code Tags - A Guide

Then, you are running a script in “/home/pe1800/bin/avg.sh” as root!

NEVER do that.

If root runs a script of him, he places it in “/root/bin/avg.sh”, never
in a directory under the control of a plain user (even if it is you).

If a user wants to run his own script, he edits his crontab, not
root’s crontab.

Lastly, emails sent to root are typically forwarded to the main user of
your system.

> I did a
> /var/spool/cron/tabs # cat root and everything is there. I also did an
> /etc cat crontab and, as you said, it hasn’t changed. That’s a mystery
> to me but, after your warning, I didn’t touch it. Will it, perhaps,
> change after rebooting?

No, it will not change.

> But what troubles me is when checking the mail it said I had - under MY
> userid - there was a message: /bin/sh: root: command not found.
> SHELL=/bin/sh was and still is the very first line in crontab, and I
> never touched it! How come?

The paragraph above is not clear to me. Please repost as it is in the
original, inside code tags. Like this:


cer@Telcontar:~> l /bin/sh
lrwxrwxrwx 1 root root 4 Aug 19  2012 /bin/sh -> bash*
cer@Telcontar:~>
/

The command and its output, complete, unchanged.

Now, a bit of an explanation. You can find this in the manuals in
complete detail.

There are several crontabs in the system.

You have the users crontabs, and root is another user. They are all
under the “/var/spool/cron/tabs/” directory.

You have the system main crontab, which is the file “/etc/crontab”. If
you need to change this one, you do that with an editor, directly.

You have the system crontab files, placed in the “/etc/cron.d/”
directory. The name is arbitrary, the syntax is a bit different, and
they are edited directly with an editor. Or, they are simply dropped
there by a package.

Lastly, in an openSUSE system, you also have the directories:

/etc/cron.hourly/
/etc/cron.weekly/
/etc/cron.monthly/

where you can place scripts that run once in the indicated period (you
do not control exactly when).

Other Linux system might use anacron instead.


Cheers / Saludos,

Carlos E. R.
(from 12.1 x86_64 “Asparagus” at Telcontar)

On 05/28/2013 08:16 PM, pe1800 pecked at the keyboard and wrote:
>
>
> But what troubles me is when checking the mail it said I had - under MY
> userid - there was a message: /bin/sh: root: command not found.
> SHELL=/bin/sh was and still is the very first line in crontab, and I
> never touched it! How come?
>
>

You don’t need to put root in the crontab entry and as you found out you
got an error. The reason is because cron was trying to run a
file/command named “root”. Just remove the word root from the line and
it should execute your script.

Ken

On 2013-05-29 05:35, Ken Schneider wrote:
> On 05/28/2013 08:16 PM, pe1800 pecked at the keyboard and wrote:
>>
>>
>> But what troubles me is when checking the mail it said I had - under MY
>> userid - there was a message: /bin/sh: root: command not found.
>> SHELL=/bin/sh was and still is the very first line in crontab, and I
>> never touched it! How come?
>>
>>
>
> You don’t need to put root in the crontab entry and as you found out you
> got an error. The reason is because cron was trying to run a
> file/command named “root”. Just remove the word root from the line and
> it should execute your script.

You are right.

I think I understand. This comes from him not using code tags. Ok, he
wrote the line:


7 11 * * * root /home/pe1800/bin/avg.sh

and used “sudo crontab -e” to create the crontab. The syntax of the line
above is the correct syntax for crontab files placed under
“/etc/cron.d/”, but not for normal crontab files as this one.

On previous posts I said the syntax was different there - well, the
difference is precisely that you add the user name just there.

On a normal crontab, it happens just what you say: it is trying to run a
command named “root”.

If you read the wrong manual page, or the wrong paragraph, you get the
wrong info - man pages are not easy to read :wink:


Cheers / Saludos,

Carlos E. R.
(from 12.1 x86_64 “Asparagus” at Telcontar)