0

I realize this is frowned upon. Many will suggest they don't recommend doing a server restart from cron because it's not being monitored. In my case I would like to do this, the reason is because the server is used for only one task, in a node server environment. The server runs out of memory and I've been advised to just restart the whole server. I'm ok with this.

Currently in my cron file I have the following. (I got this off another stack question) .

0 */4 * * * /usr/bin/reboot

However it doesn't work. When I manually go to this directory /usr/bin/ then type "reboot" it works. But executing like this /usr/bin/reboot from chron doesn't restart the server.

When I do ls /usr/bin/reboot the return is /usr/bin/reboot When I do which reboot the return is /sbin/reboot

How would I restart the server on centos 6 using cron?

FabricioG
  • 167
  • 1
  • 7
  • Thanks for commenting. How is the answer posted related to this? I'm trying to restart the server from cron. The answer marked as duplicate is how to restart cron, not the server. @MichaelHampton – FabricioG Jun 21 '19 at 17:48
  • "When I manually go to this directory /usr/bin/ then type "reboot" it works. " Things do not work like you think, or at least in general. Without a path, the shell looks at the `$PATH` variable to find out where is the command you are trying to execute. So `reboot` may well be somewhere else. Do `ls /usr/bin/reboot` and if you get an error you have your explanation on why it does not work. Try `which reboot` to find out where it is really. Remember also that in theory maybe only `root` can use it so it has to be in `root`'s crontab. – Patrick Mevzek Jun 21 '19 at 17:54
  • ls /usr/bin/reboot returns this: ls /usr/bin/reboot /usr/bin/reboot which reboot returns this: /sbin/reboot @PatrickMevzek – FabricioG Jun 21 '19 at 17:57
  • Would answer be this then: 0 */4 * * * /sbin/reboot? @PatrickMevzek – FabricioG Jun 21 '19 at 17:59
  • In general, " In my case I would like to do this (with out going into explanations why)." is not exactly the best way to ask for help. Because you may have your reasons indeed, but they may as well come from false assumptions or other things that could be solved differently (aka the X/Y problem). In theory, detailing your assumptions/constraints fully is better to get a better answer. Example (silly but at least descriptive): I want to reboot it daily to test it can survive a power failure and reboot successfully. – Patrick Mevzek Jun 21 '19 at 17:59
  • Ok fixing @PatrickMevzek – FabricioG Jun 21 '19 at 18:01
  • 1
    I updated with a brief description of why. I had put what I did because on another question it became more a lecture of why you shouldn't restart from cron., but thanks for the heads up! @PatrickMevzek – FabricioG Jun 21 '19 at 18:06
  • 1
    If your web app has a memory leak, then that's what you need to restart. At least until the developer fixes it. Reboting the whole machine doesn't make sense in this context. – Michael Hampton Jun 21 '19 at 18:20
  • I did but it doesn't fix it. That's why the easiest thing would be to restart the whole server. For me I'm ok with this. @MichaelHampton – FabricioG Jun 21 '19 at 18:22

1 Answers1

1

As a commenter mentioned, you're calling the wrong program. The reboot binary is at /sbin/reboot, not at /usr/bin/reboot. The latter is a symbolic link to consolehelper, which is a wrapper that lets non-root users run the corresponding program in /sbin under certain circumstances. You can see its man page if you're really interested in how it works, but as CentOS 6 will go end of life soon, and current versions do not use it, it's probably not worth worrying about. Just put in the correct path and go on to more important things, like fixing the memory leak.

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972