Use crontab to restart an Upstart service on schedule?

27,263

You're on the correct path adding this to the root user's cron.

Full system paths

When using cron, you want to be sure to use full system paths for commands and files. You can use the which command to find out where restart is located: which restart. For me, it shows /sbin/restart.

So, using this information, the cron would look as follows:

0 6 * * * /sbin/restart shiny-server

which would restart the server at 6:00AM system-time.

Output to /dev/null

When cron runs, it will e-mail any output to the user who is running the cron jobs. If you don't want this, which you probably don't, the command you'd run would look like this:

0 6 * * * /sbin/restart shiny-server > /dev/null 2>&1

This will output both stdout and stderr to /dev/null, instead of being read by cron.

Edit

I originally misunderstood and thought that this service was runing using init, when it is in fact using upstart. The moral of the story remains use full system paths when using cron.

Share:
27,263

Related videos on Youtube

Ray
Author by

Ray

Updated on September 18, 2022

Comments

  • Ray
    Ray over 1 year

    I need to restart shiny-server (Upstart service) on Ubuntu 12.04 on a routine schedule.

    Typically, if I want to restart this service manually, I simply type: sudo restart shiny-server or service shiny-server restart (as root). As I understand, this is similar to something like sudo restart mysql.

    To schedule the restart, I tried the following entries in my root user's crontab:

     0 6 * * * restart shiny-server
     0 6 * * * service shiny-server restart
     0 6 * * * bash /home/local/ANT/raybao/dev/script.sh
    

    Where the last line of script.sh is service shiny-server restart.

    None of the above actually works!

    So I'm pretty stumped/confused. What am I missing? Thanks!!

    More info for shiny-server here: http://rstudio.github.io/shiny-server/latest/#upstart-ubuntu-12.04-through-14.10-redhat-6

    • Kevin
      Kevin about 7 years
      As the answer below notes, /usr/sbin/service can't be called from cron for some reason, but /sbin/restart service_name does work.
  • Ray
    Ray over 8 years
    When I typed which shiny-server, I got /usr/bin/shiny-server. So thus, I added the line 30 13 * * * /usr/bin/service shiny-server restart (testing for 1:30pm), and unfortunately, it did not restart the service as expected. Any ideas on what might be wrong?
  • Ray
    Ray over 8 years
    I also added the same line to the end of my script.sh and tried calling that in my crontab as well. script.sh contains add'l MySQL commands (which ran successfully), but the last line to restart the service did not.
  • earthmeLon
    earthmeLon over 8 years
    You're misunderstanding. We don't care where shiny-server is. We care where service is. Please try the answer again with that in mind. I gave you what should be a working example :D
  • earthmeLon
    earthmeLon over 8 years
    I am correct that I read that sudo service shiny-server restart successfully restarts the service? In that case, I think that the examples provided should work.
  • Ray
    Ray over 8 years
    Hmm... I added the exact line you posted in your solution, except changed sbin to bin (because that's where which service returned), but it still did not work...
  • earthmeLon
    earthmeLon over 8 years