LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Fedora
User Name
Password
Fedora This forum is for the discussion of the Fedora Project.

Notices

Reply
 
Search this Thread
Old 10-03-2012, 11:16 AM   #1
homer_3
Member
 
Registered: May 2008
Posts: 85

Rep: Reputation: 15
How do I run a cronjob as root?


I've added 3 shell scripts to my cronjobs. 2 of them run and work fine. The cron log shows the 3rd also runs, but it doesn't do what it's supposed to do. If I run the 3rd script myself, on the command line, it works fine.

The script that isn't working just deletes some old files. But those files are owned by root. So I'm guessing that the scrip isn't running as root from cron. From Googling, it looks like I'm supposed to run crontab -e as root and set the script to run. But that's what I'm doing. Am I missing some other step?
 
Old 10-03-2012, 11:42 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967
You're probably not missing anything. Personally I'd run the files from /etc/crontab, which is broadly the same as the root crontab, but I'd describe the difference as the system vs the root user, which is basically the same thing technically.

more often it's a case of the script not having a suitable interpreter in the script or such like, what is the script? You might find it simpler to drop the whole thing into /etc/cron.daily or some such location. That's my preferred option, if you need to run it on a common schedule that already exists in /etc/crontab
 
Old 10-03-2012, 12:06 PM   #3
homer_3
Member
 
Registered: May 2008
Posts: 85

Original Poster
Rep: Reputation: 15
The script is very simple. It doesn't run any perl or bash logic or anything fancy like that. It just uses some standard executables. Basically, files are backed up every day. But I only want to keep a few days worth of backups. So the script just finds the old ones and deletes them.

Code:
find /root/backups/ -name *.tar.gz -mtime +4 -exec rm -f {} \;
 
Old 10-03-2012, 02:15 PM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967
right so you'd want a shebang before it, #!/bin/sh so you have a full environment to work with. It's probably that /usr/bin is not on the path so it can't find the find command.
 
Old 10-03-2012, 02:39 PM   #5
homer_3
Member
 
Registered: May 2008
Posts: 85

Original Poster
Rep: Reputation: 15
I've tried a bunch of different stuff like that, but nothing worked. I've tried prepending "/bin/" to find and shebangs #!/bin/sh, #!/bin/bash, #!/bin/env sh, and #!/bin/env bash.
 
Old 10-03-2012, 02:45 PM   #6
Kropotkin
Member
 
Registered: Oct 2004
Location: /usr/home
Distribution: Fedora, Mint, FreeBSD, Android
Posts: 347

Rep: Reputation: 31
Have you tried putting it here? /etc/cron.daily
 
Old 10-03-2012, 02:51 PM   #7
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,141

Rep: Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638
Quote:
Originally Posted by homer_3 View Post
Code:
find /root/backups/ -name *.tar.gz -mtime +4 -exec rm -f {} \;
maybe this mite work:
Code:
/usr/bin/find /root/backups/ -name *.tar.gz -mtime +4 -exec /usr/bin/rm -f {} \;
 
Old 10-03-2012, 10:36 PM   #8
frankbell
Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Mageia, Mint
Posts: 8,098

Rep: Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537
I think you all are making this more complicated than it needs to be.

To run a script as a cronjob with root permissions, create and test your script as root.

Once you've verified that the script works without error, run crontab -e in a terminal as root and edit the crontab file to add your script to the crontab (cron table).

See man crontab for more information.
 
Old 10-04-2012, 02:47 AM   #9
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967
Quote:
Originally Posted by frankbell View Post
I think you all are making this more complicated than it needs to be.

To run a script as a cronjob with root permissions, create and test your script as root.

Once you've verified that the script works without error, run crontab -e in a terminal as root and edit the crontab file to add your script to the crontab (cron table).

See man crontab for more information.
No, i don't at all. it's very possible to have a script running in a full bash environment that chokes in cron.
 
Old 10-04-2012, 11:12 AM   #10
homer_3
Member
 
Registered: May 2008
Posts: 85

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by frankbell View Post
I think you all are making this more complicated than it needs to be.

To run a script as a cronjob with root permissions, create and test your script as root.

Once you've verified that the script works without error, run crontab -e in a terminal as root and edit the crontab file to add your script to the crontab (cron table).

See man crontab for more information.
This is exactly what I've done. I've set several other scripts up the same way. This one is not working. I'm not sure what the issue is, perhaps find with exec doesn't work with cron? I've written up a much more complicated shell script that does the same thing as what I had the one liner find command doing. Since the non-find script seems to be working, I guess I'll stick with it.
 
Old 10-04-2012, 11:19 AM   #11
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,141

Rep: Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638
^ did you try my suggestion (post #7) ?

i think acid mite be rite about not having a full bash environment so you are not getting a $PATH setup so it cant find the location of those commands.

Last edited by schneidz; 10-04-2012 at 11:20 AM.
 
Old 10-04-2012, 09:39 PM   #12
frankbell
Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Mageia, Mint
Posts: 8,098

Rep: Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537Reputation: 1537
Quote:
No, i don't at all. it's very possible to have a script running in a full bash environment that chokes in cron.
Thanks. Learn something every day.
 
Old 10-05-2012, 11:10 PM   #13
jefro
Guru
 
Registered: Mar 2008
Posts: 11,965

Rep: Reputation: 1483Reputation: 1483Reputation: 1483Reputation: 1483Reputation: 1483Reputation: 1483Reputation: 1483Reputation: 1483Reputation: 1483Reputation: 1483
Normally cron is to be treated as a any user. To include all permissions and to include all environmental settings. Any cron script ought to be available as if any other user ran it if all of the permissions and environment are satisfied.

http://216.147.18.102/unixfaq/cron.shtml

Last edited by jefro; 10-05-2012 at 11:13 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cronjob did not run casperdaghost Linux - Newbie 15 12-22-2011 11:48 AM
[SOLVED] Cronjob to run every other weekday linuxguy7820 Linux - Newbie 2 10-27-2011 02:38 PM
run cronjob once every three months noir911 Linux - Server 2 04-30-2010 04:30 PM
cronjob to run every 15 minutes? your_shadow03 Linux - Newbie 5 11-19-2009 11:58 PM
why do these cronjob not run? SheldonPlankton Linux - General 1 01-05-2005 09:01 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration