I have an issue with Tomcat 7 on my Ubuntu machine.

I'm still a rookie in this, but it seems that restarting the tomcat7 service fixes the issue. I would like to make a scheduled task on the machine to restart tomcat7 every 2 days.

For now I use sudo /etc/init.d/tomcat7 restart, is there an easy way to implement this?


The cron package is the normal tool for scheduling events on your system. You can put actions into your user's crontab with the crontab -e command. Although as you need to use sudo to execute the init.d script, this won't work unless you've set up sudo usage with no password for that command.

My suggestion is that you edit the root crontab, by doing sudo crontab -e, and enter the line:

30 6 */2 * * /usr/bin/service tomcat7 restart

This will restart tomcat7 at 6:30am every 2 days. If you want a different time, just alter the first two numbers. The command probably looks different to what you'd expect, but in 14.04 the correct way to use scripts in /etc/init.d is to use the service command. More information on cron usage can be found here.


You can use a cron job for this. Create a shell script with these:

/etc/init.d/tomcat7 restart

Then set up a job in the crontab:

0 0 */2 * * root my_script.sh

This will run the script at midnight, every two days.

This will "solve" your immediate problem. However I suggest to fix your application running on Tomcat, it is hardly ready for production, if it needs restarts like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.