0

I have a few cron jobs at server machine scheduled to do few things. I want to let user modify those cron jobs using a web application (jsp page). So lets say (example scenario) some job is scheduled to run at 2 PM, I want user the option to change its time.

It looked very trivial to start with, but now I am stuck. I am new to Linux. How to create cron jobs currently is : crontab - e ; and then I manually add new jobs as required. But I want to provide this capability to a remote user through a web interface.

Please help !

Andy897
  • 6,915
  • 11
  • 51
  • 86
  • So what I indirectly mean how to modify an existing cron job using a script which I can call using my servlet. Similar question already posted http://stackoverflow.com/questions/4880290/linux-how-do-i-create-a-crontab-thru-a-script . But no satisfactory working solution. :( – Andy897 Nov 17 '12 at 20:28

1 Answers1

1

Let's say that your crontab has a following line:

20 3 * * * /home/somebody/somescript.sh

You can list your crontab using the following command:

crontab -l

Then you can change the scheduled time using the command sed:

sed 's/20 3 \* \* \* \/home\/somebody\/somescript.sh/30 4 \* \* \* \/home\/somebody\/somescript.sh/'

Finaly, you'll commit it to crontab again by passing the new file to the crontab command.

The result will be combination of the three commands discussed above and will be connected by pipes:

crontab -l | sed 's/20 3 \* \* \* \/home\/somebody\/somescript.sh/30 4 \* \* \* \/home\/somebody\/somescript.sh/' | crontab

The first command will list the current crontab to standard output. Then sed will replace the time and pass the replaced file to the crontab command, which will install it as a new crontab.

Be aware though, that most servlets do not have sufficient system rights to create or modify crontab.

px1mp
  • 5,212
  • 1
  • 18
  • 10