Use crontab to restart an Upstart service on schedule?
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
.
Related videos on Youtube
Ray
Updated on September 18, 2022Comments
-
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
orservice shiny-server restart
(asroot
). As I understand, this is similar to something likesudo restart mysql
.To schedule the restart, I tried the following entries in my
root
user'scrontab
: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
isservice 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 about 7 yearsAs the answer below notes, /usr/sbin/service can't be called from cron for some reason, but /sbin/restart service_name does work.
-
-
Ray over 8 yearsWhen I typed
which shiny-server
, I got/usr/bin/shiny-server
. So thus, I added the line30 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 over 8 yearsI also added the same line to the end of my
script.sh
and tried calling that in mycrontab
as well.script.sh
contains add'l MySQL commands (which ran successfully), but the last line to restart the service did not. -
earthmeLon over 8 yearsYou're misunderstanding. We don't care where
shiny-server
is. We care whereservice
is. Please try the answer again with that in mind. I gave you what should be a working example :D -
earthmeLon over 8 yearsI 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 over 8 yearsHmm... I added the exact line you posted in your solution, except changed
sbin
tobin
(because that's wherewhich service
returned), but it still did not work... -
earthmeLon over 8 yearsLet us continue this discussion in chat.