Register
It is currently Tue Jul 22, 2014 1:30 pm

Removing a line (job) containing certain string from crontab


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Wed Aug 08, 2012 4:43 am   

Joined: Wed Aug 08, 2012 4:28 am
Posts: 2
Hello guys

I googled myself up and down the whole internet about this thing without any working findings:

What I have: A bash script that is generating other bash script accoring to some input by the user. The same script writes a new cronjob into crontab for each generated script.

What I need: I need to delete a cronjob that contains a certain string (user input in variable)...

What I had so far:

sed -i '/$inputbyuser/ d' /etc/crontab #not working
crontab -l | [sed /$inputbyuser/d] #not working
...


Any Ideas how I can delete a line in crontab?
Thanks a lot in advance


Top
 Profile  
 PostPosted: Wed Aug 08, 2012 8:04 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Code:
printf '%s\n' "g/${inputbyuser}/d" w | ed -s /etc/crontab


Top
 Profile  
 PostPosted: Wed Aug 08, 2012 8:59 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 532
hi,

Code:
sed -i "/$inputbyuser/ d" /etc/crontab #should work: variables are not expanded inside single quotes.
crontab -l | sed /$inputbyuser/d #what were the square brackets for?


Top
 Profile  
 PostPosted: Wed Aug 08, 2012 9:48 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
I think he was trying to guess the syntax and used Tcl's.

By the way, you should check what the user input is before processing it.
What if it's « . » for example? It'll clear everything since it represents a character.

Tricky task, I know.

What about the following?

Code:
sed -i "\,$(printf %q "${inputbyuser//./\.}"),d" /etc/crontab


I think it suffices. It's not completely safe though.



EDIT:

Improved to keep dots.

Code:
tmp=$(printf %q "${inputbyuser//./\.}")
inputbyuser=${tmp//\\\\/\\}

sed -i "\,${inputbyuser},d" /etc/crontab



EDIT (2):

Code:
tmp=$(printf %q "${inputbyuser//./\.}")
inputbyuser=${tmp//\\\\/\\}

sed -i \\$'\10'"${inputbyuser}"$'\10'd  /etc/crontab


I think it should be good now.


Top
 Profile  
 PostPosted: Thu Aug 09, 2012 12:28 am   

Joined: Wed Aug 08, 2012 4:28 am
Posts: 2
jsz wrote:
Code:
printf '%s\n' "g/${inputbyuser}/d" w | ed -s /etc/crontab


Thank you so much jsz. This works perfect.

jsz wrote:
By the way, you should check what the user input is before processing it.
What if it's « . » for example? It'll clear everything since it represents a character.

Tricky task, I know.


Under normal circumstances I'd agree with you. Since the whole Software will be used by only 2-3 Sysadmins it's fine like it is. My security here is that I backup the crontab before making the changes to it. So there's a "crontab history". Besides that, the user will see his input again and has to verify it.

Maybe I have to elaborate what this project is. I've automated a Switch and Router Backupsystem through Rancid. The program collects all configs directely from the hardware and backs them up on a virtual Linux server. After that the backups will be transfered via scp to the actual data server. At the moment there are over 100 switches and router so I thought its easier to make a small script to apply changes, rather then set up folders / scripts and cronjobs by hand. My script creats all directories, creats custom scripts for the login procedure and makes a new cronjob for the script. Thanks to you within a few seconds the user is now able to move these various files to quarantine, even the cronjob will be written into a file before it gets extracted from the crontab. This quarantine deletes files after a certain age in days.

So thanks again for your help jsz as well as Watael.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP