LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-23-2012, 10:36 AM   #1
ac_kumar
Member
 
Registered: Aug 2011
Distribution: Ubuntu, Fedora
Posts: 175

Rep: Reputation: 9
set crontab from a bash script


Hi i want to set a cronjob from shell script
which i can do from command line as:-
#crontab -e
12 2 * * * /path/to script

How can i set crontab as above from shell script.
 
Old 03-23-2012, 10:40 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You may try a here document:
Code:
crontab << EOS
12 2 * * * /path/to script
EOS
 
1 members found this post helpful.
Old 03-23-2012, 12:29 PM   #3
ac_kumar
Member
 
Registered: Aug 2011
Distribution: Ubuntu, Fedora
Posts: 175

Original Poster
Rep: Reputation: 9
Quote:
Originally Posted by colucix View Post
You may try a here document:
Code:
crontab << EOS
12 2 * * * /path/to script
EOS
I tried the code in terminal it is working thanks for help
but when i put it in a script and try to run it,it show permission denied error. And what EOS means.
 
Old 03-23-2012, 12:44 PM   #4
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,232

Rep: Reputation: 299Reputation: 299Reputation: 299
EOS is just a marker. It can be anything you like. Some use EOF..

Did you chmod +x the script?
 
Old 03-23-2012, 12:54 PM   #5
ac_kumar
Member
 
Registered: Aug 2011
Distribution: Ubuntu, Fedora
Posts: 175

Original Poster
Rep: Reputation: 9
Quote:
Originally Posted by dive View Post
EOS is just a marker. It can be anything you like. Some use EOF..

Did you chmod +x the script?
Yes i have chmod +x the script.
Thanks for EOS explaination.
 
Old 03-23-2012, 01:23 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,332
Blog Entries: 55

Rep: Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533
A users crontab may already exist so IMHO best first list contents then add to it:
Code:
export TMPDIR=/tmp
TMPFILE=`mktemp crontab.XXXXXXXXXX` && {
 crontab -l > "${TMPFILE}"
 echo '12 2 * * * /path/to script' >> "${TMPFILE}"
 crontab "${TMPFILE}"
 rm -f "${TMPFILE}"
}
 
2 members found this post helpful.
Old 03-23-2012, 04:48 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by unSpawn View Post
A users crontab may already exist so IMHO best first list contents then add to it:
Uh oh... you're absolutely right, thanks for the add-on!

@ac_kumar: regarding here documents, take a look at http://linuxcommand.org/wss0030.php (here called here scripts).
 
Old 03-24-2012, 09:40 AM   #8
rohitchauhan
Member
 
Registered: Nov 2010
Distribution: RedHat
Posts: 88

Rep: Reputation: 1
I think EOS means END OF SCRIPT !
may be same as EOF (End Of File) in C/C++ programming.
 
Old 03-25-2012, 11:26 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by rohitchauhan View Post
I think EOS means END OF SCRIPT !
may be same as EOF (End Of File) in C/C++ programming.
As already mentioned the token in a here document can be any word, even a random sequence of characters. Said that, I use EOS as end of statements, that has more sense than end of file or end of script in this context!
 
Old 03-25-2012, 12:35 PM   #10
ac_kumar
Member
 
Registered: Aug 2011
Distribution: Ubuntu, Fedora
Posts: 175

Original Poster
Rep: Reputation: 9
thanks all for your help.
I am have tested script running with sudo command.
 
  


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
crontab and a bash script seefor Linux - General 6 01-27-2009 04:41 PM
removing # from crontab (bash script) inderpunj Programming 4 10-13-2008 03:34 PM
Bash script not running within crontab job WrightExposure Linux - General 3 01-23-2007 07:28 PM
Using crontab in a bash script nro Linux - Newbie 3 08-26-2005 09:04 PM
bash script for database searching using crontab saurya_s Linux - Software 5 01-22-2004 09:53 AM


All times are GMT -5. The time now is 09:15 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration