[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: auto restarting in crontab



On 16/03/2023 09:32, Greg Wooledge wrote:
On Wed, Mar 15, 2023 at 08:00:20PM -0500, Nicholas Geovanis wrote:
On Wed, Mar 15, 2023, 7:56 PM <coreyh@free.fr> wrote:
> My script for monitoring Node.js app as follows. I put it in crontab for
> auto-check and restart if failure.

There's so much wrong with that.

If you want to manage a service, the *best* thing you could do would be
to write a systemd unit for it (either a system-wide unit, or a --user
unit, your choice). Let systemd start it, restart it automatically when
it dies if that's what you want, and so on.

If that's more than you want to tackle, and if all you want is
automatic restarting (not the ability to stop it at will), then this
should suffice:

#!/bin/sh
PATH=/whatver/you/need
while true; do
    serve -s /path/to/your/service
    sleep 5
done

Then arrange for this script to be executed at boot time, and that's it.
No background stuff, no polling from crontab.  Just a simple loop.



Thanks Greg. I will update with the way you gave.
where will I setup this script for systemd job? any reference?

regards



>    nohup serve -s  /home/myUsername/workspace/xxx-frontend/build &

This will log stdout and stderr in nohup.out in the working directory.
Look for errors there.  I bet it's a PATH issue.

Cron jobs run in a sanitized environment and may not be running with the
permissions you have as root on the command line.

I doubt the permissions are different. Probably just PATH and maybe some
other environment variables that the Javascript program expects, but
which are not present in cron's environment.


Reply to: