LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   set crontab from a bash script (https://www.linuxquestions.org/questions/linux-newbie-8/set-crontab-from-a-bash-script-936043/)

ac_kumar 03-23-2012 09:36 AM

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.

colucix 03-23-2012 09:40 AM

You may try a here document:
Code:

crontab << EOS
12 2 * * * /path/to script
EOS


ac_kumar 03-23-2012 11:29 AM

Quote:

Originally Posted by colucix (Post 4634455)
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.

dive 03-23-2012 11:44 AM

EOS is just a marker. It can be anything you like. Some use EOF..

Did you chmod +x the script?

ac_kumar 03-23-2012 11:54 AM

Quote:

Originally Posted by dive (Post 4634539)
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.

unSpawn 03-23-2012 12:23 PM

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}"
}


colucix 03-23-2012 03:48 PM

Quote:

Originally Posted by unSpawn (Post 4634577)
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! :cool:

@ac_kumar: regarding here documents, take a look at http://linuxcommand.org/wss0030.php (here called here scripts).

rohitchauhan 03-24-2012 08:40 AM

I think EOS means END OF SCRIPT !
may be same as EOF (End Of File) in C/C++ programming.

colucix 03-25-2012 10:26 AM

Quote:

Originally Posted by rohitchauhan (Post 4635095)
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! :)

ac_kumar 03-25-2012 11:35 AM

thanks all for your help.
I am have tested script running with sudo command.


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