how to set crontab entries through shell scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to set crontab entries through shell scripting
# 1  
Old 07-07-2009
how to set crontab entries through shell scripting

Hi,
I am not that good in shell scripting...
I would like to know how to write a shell script to add entries in to the crontab?

Can any one post me the sample code.....


Regards
Srinivasan.
# 2  
Old 07-07-2009
This is what we use as a postinstall to create a crontab

Code:
USER=`/usr/ucb/whoami`
CRONLOC=/var/spool/cron/crontabs
CRONCOMMFILE=/tmp/cron_comm_file.sh
CRONGREP=`crontab -l | grep sys_diag`


if [ "$CRONGREP" != "" ];
then
echo "Crontab already exists"
exit 
fi
#
# Create command file to create crontab file
#
# If command files already exists delete it
#
if [ -f $CRONCOMMFILE ]; then
  rm -f $CRONCOMMFILE
fi

CRONLINE="0 9 * * 1,3,5 /var/tmp/sys_diag -l -a -o `hostname` -C"
CRONLINE2="5 9 * * * /var/tmp/clean"

#
# Added commands into the file to create the crontab entry
#

echo "#!/bin/sh" >> $CRONCOMMFILE
echo "EDITOR=ed" >> $CRONCOMMFILE
echo "export EDITOR" >> $CRONCOMMFILE


echo "crontab -e $USER << EOF > /dev/null" >> $CRONCOMMFILE
echo "a" >> $CRONCOMMFILE
echo "$CRONLINE" >> $CRONCOMMFILE
echo "$CRONLINE2" >> $CRONCOMMFILE
echo "." >> $CRONCOMMFILE
echo "w" >> $CRONCOMMFILE
echo "q" >> $CRONCOMMFILE
echo "EOF" >> $CRONCOMMFILE

chmod 750 $CRONCOMMFILE

#
# Execute the the crontab command file
#
if [ -f $CRONCOMMFILE ]; then
  $CRONCOMMFILE
  rm $CRONCOMMFILE
else
  echo "Error: File generated to create crontab entry does not exist"
  exit 1
fi

echo "Crontab Entry Inserted Successfully"
#

# 3  
Old 07-07-2009
Hi thanks so much for the help!

One more question?

How to remove the entry from the crontab if i dont need that thro shell scripting?

Thanks in advance
Srinivasan
# 4  
Old 07-07-2009
IF you want to remove the CRON you need to use crontab -e to remove the line you entered
but you need to set your editor as su
EDITOR=vi
export EDITOR
crontab -e

I am not 100% on removing the CRON tab entry with a script as I do not use a shell script to create a one time crontab.
# 5  
Old 07-07-2009
I need the removal of crontabl entry thro shell scripting..

Could you explain me in detail?

Srini
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to Set crontab for 4th and 25th of every month?

Hello, I wanted to set Crontab for 4th and 25th of every month at 5:00 PM. Script should take previous month and current year as command line arguement like... /home/test1.sh -f ABCD 03 2014 so above script will run on 4th and 25th April 2014 but argument should be like previous month... (11 Replies)
Discussion started by: ajju
11 Replies

2. Shell Programming and Scripting

Help needed with shell script to search and replace a set of strings among the set of files

Hi, I am looking for a shell script which serves the below purpose. Please find below the algorithm for the same and any help on this would be highly appreciated. 1)set of strings need to be replaced among set of files(directory may contain different types of files) 2)It should search for... (10 Replies)
Discussion started by: Amulya
10 Replies

3. UNIX for Dummies Questions & Answers

How to set Subject in Sendmail in Crontab

Hello All, I am trying to send a file through email everyday from one of my Linux (x86_64) system. I've scheduled Sendmail in Crontab like this- 00 8 * * * /usr/sbin/sendmail name@xxx.co.uk < /home/file1.out This entry in crontab is working very fine and I am receiving file file1.out... (10 Replies)
Discussion started by: NARESH1302
10 Replies

4. Shell Programming and Scripting

How to set crontab for different Time Zone

Hi, I want to set cron job for different time zone from my machine. So here is what I did to set it. I am having a file cronfile, which I use to set cron jobs by using Crontab cronfile Now in cronfile I set TZ variable as Set TZ=Asia/Calcutta But now... (3 Replies)
Discussion started by: sarbjit
3 Replies

5. Shell Programming and Scripting

Shell script to generate daily crontab entries between to specific dates

Hi, I am currently writing a shell script to enter daily reoccurring jobs into the crontab. I want to be able to specify any two dates for which I want these daily jobs to run between. Below is an example of what I need to be written to crontab. # Give a start day of 21/10/2009 09:00... (2 Replies)
Discussion started by: JM_86
2 Replies

6. UNIX for Dummies Questions & Answers

Re : Set multiple cron jobs in one crontab file

Hello All, Hopw all is fine. I am newbie to Unix. I am using Bourne Shell (sh). One of the question I have is that I am trying to read XML file and based on reading that XML file I want to run different java programs at different hours. Meaning 05 14 * * * java ./program1 10 14 * * * java... (3 Replies)
Discussion started by: samshaw
3 Replies

7. UNIX for Dummies Questions & Answers

Usage of set in shell scripting

I am not able to understand the mentioned usage of set. $A set $l Please reply ASAP. Need to fix sth in my code. Thanks in advance. (0 Replies)
Discussion started by: trichyselva
0 Replies

8. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies

9. Shell Programming and Scripting

bash scripting cannot executed in crontab

hi guys, i have a problem. a week ago i made a successful crontab that execute bash scripting daily, it worked well but now, it doesn't work at all, in the mail i have: " /home/jimmy/cha/scripts/cekpderr produced the following output: lagi jalan /home/jimmy/cha/scripts/cekpderr:... (6 Replies)
Discussion started by: jimmbp
6 Replies
Login or Register to Ask a Question