Visit Our UNIX and Linux User Community


Commenting contab from a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Commenting contab from a script
# 1  
Old 10-01-2009
Commenting contab from a script

Dear All,
I have many cron entries and want to comment a particular cron entry from a script.
Ex- Suppose I have the below cron entries:
Code:
# DO NOT EDIT THIS FILE - edit the master and reinstall.
#Cron entries for Jboss server 1
0 23 * * * /usr/bin/echo
0 23 * * * /usr/bin/asdg_count.sh
0 23 * * * /usr/bin/true
0 23 * * * /usr/bin/true

I want to comment the entry for asdg_count.sh from my script
I am using the below command:
Code:
(echo ^[:g/watchdog/s/^/#/^[:wq!^M)|crontab -e >> temp.txt

But the command is not working
and I am getting the console message as :
Code:
Vim: Warning: Output is not to a terminal
Vim: Warning: Input is not from a terminal
crontab: "vi" exited with status 1

and the message in temp.txt is :
Code:
-- Ip/crontab.XXXXOKeTQL" 7L, 305C
Vim: Error reading input, exiting...
Vim: preserving files...
Vim: Finished.

Please help on commenting the cron from the script.


Thanks
AK

Last edited by Franklin52; 10-02-2009 at 05:33 AM.. Reason: Please use code tags!
# 2  
Old 10-01-2009
One way which may help you.,

1. Backup the crontab as

crontab -l >> tmpfile.txt

2. Use sed with -i option to substitute.

3. and then remove all crontab.

crontab -r

4. install the new file as

crontab tmpfile.txt

It will work best for you.... instead of trying to do the edition with vim.
Also, note that, if some body else edits that crontab at the same time then you will end up in mess..
# 3  
Old 10-01-2009
Quote:
Originally Posted by avishek007
Dear All,
I have many cron entries and want to comment a particular cron entry from a script.
Ex- Suppose I have the below cron entries:
# DO NOT EDIT THIS FILE - edit the master and reinstall.
#Cron entries for Jboss server 1
0 23 * * * /usr/bin/echo
0 23 * * * /usr/bin/asdg_count.sh
0 23 * * * /usr/bin/true
0 23 * * * /usr/bin/true
I want to comment the entry for asdg_count.sh from my script

Code:
crontab -l | sed '/asdg_count.sh/ s/^/#/' | crontab -


Last edited by cfajohnson; 10-02-2009 at 02:44 PM.. Reason: Missing apostrophe added (thanks methyl)
# 4  
Old 10-02-2009
crontab -e | sed '/watchdog/ s/^/#/ | crontab -

I guess its not complete ...


please help
# 5  
Old 10-02-2009
crontab -e, complete or not, is wrong!

crontab -l was mentioned, though.

I would be cautious of automating anything with the crontab file without taking a backup first.

Code:

crontab -l | tee crontab.bak | sed '/asdg_count.sh/ s/^/#/' | crontab -


Last edited by Scott; 10-02-2009 at 05:03 PM.. Reason: good spot, methyl.... added closing apostrophe
# 6  
Old 10-02-2009
avishek007 the example you post is looking for a line in crontab containing the word "watchdog" not "asdg_count.sh".

There is a minor typo in cfajohnson's post (missing quote) which has been carried forward into the next two posts. Perhaps that is the problem?

Code:
crontab -l | sed '/asdg_count.sh/ s/^/#/' | crontab -

I would always advise copying crontab to a safe area before editing the file. The rest really depends on local rules.

Previous Thread | Next Thread
Test Your Knowledge in Computers #1006
Difficulty: Medium
In calculus and most other branches of mathematics beyond practical geometry, angles are universally measured in degrees.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Commenting out a cron entry through a shell script

In my cron thare is a line like 24 11 * * * /usr/batch/bin/abc.sh > /usr/batch/log/abc.log 2>&1 along with other entries. I want to comment out this line through a shell script. My local variable 'line'ontains the full entry (i.e. 24 11 * * * /usr/batch/bin/abc.sh > /usr/batch/log/abc.log... (4 Replies)
Discussion started by: Soham
4 Replies

2. Shell Programming and Scripting

Commenting Multiple lines using Shell Script

I have an xml file which has following code : <abc-ref> <abc-name>abc.efg.hij.klm</abc-name> </abc-ref> I want to comment this whole section out and I have written the following script : (where "hij" is unique string in the file) TEMPFILE=replaceYY.tmp file=hello.xml sed -n... (6 Replies)
Discussion started by: Dish
6 Replies

3. Shell Programming and Scripting

Commenting lines in Shell script

Hi All, I know we can comment by using "#" .... I want to know... is there any way to comment a whole big script easily.... In a file i need to comment more than 15 lines ........ and check the script and un comment back. I am learning VI now so its taking lot of time to comment and un... (4 Replies)
Discussion started by: firestar
4 Replies

4. Shell Programming and Scripting

commenting out lines between two delimiters

Hi All, I am struggling to get my head around the following issue. I am having to comment out lines between two delimiters by placing an asterix in position 7 but retain all lines in the file and in the same order. so for example a file containing: ... ... DELIM1 ... ... DELIM2... (2 Replies)
Discussion started by: Bruble
2 Replies

5. Shell Programming and Scripting

Commenting lines

Hi can any body pls help me : I have a file Which Content is like following: p3:s1234:powerfail:/usr/sbin/shutdown -y -i5 -g0 >/dev/msglog 2<>/dev/msglog ca:3:respawn:/opt/GoldWing/currentPM/local/critagt > /dev/msglog 2<>/dev/msglog ca:3:respawn:/opt/GoldWing/currentPM/local/startcia.sh... (2 Replies)
Discussion started by: Aditya.Gurgaon
2 Replies

6. Shell Programming and Scripting

commenting

can we use "---------" for commenting......... (2 Replies)
Discussion started by: simmijaswal
2 Replies

7. Shell Programming and Scripting

Commenting a Line In a File

HI all I am working on a script, few details are as follows. I have one properties File and one script. The property file contains the JOBID which are to be executed and the Script runs these jobs ONE by ONE. After completing the JOB I need to comment that job in the property File. This is the... (3 Replies)
Discussion started by: Prashantckc
3 Replies

8. Shell Programming and Scripting

Commenting

How can i comment multiple lines in unix ..............shell script. (6 Replies)
Discussion started by: dreams5617
6 Replies

9. UNIX for Dummies Questions & Answers

Commenting lines

How to comment a set of lines in a script? we use # to comment a single line , is there ant other cmd to comment a block? (2 Replies)
Discussion started by: rolex.mp
2 Replies

10. Shell Programming and Scripting

commenting more then 1 line

Hi all, I m new for linux ... but now i will do this continuously. So my question is how to comment more then 1 line i dont wanna put # to each line.. my script is too big... or u can tell me that how can i add # to sellected lines means if i wanna put # from line number 10 to 20 then how... (3 Replies)
Discussion started by: ajayyadavmca
3 Replies

Featured Tech Videos