There is some weird behavior going on that leads me to think there may be something going on...
So I have a shell script executed by cron. Basically it is meant to check if Node.js is running. If so, log the date... if not then log nothing! At the time I built it I tested it and saw it logged when a node script was running and did not log when it stopped running...
Recently I knew Node went down and thought it was the perfect opportunity to check if the script did what its supposed to do. It didnt! And it does not... :(
Here is the script:
#!/bin/bash
if ps -Al | grep -v grep | grep -q node
then
date > /etc/nodeCheck.log
else
date > /dev/null
fi
Permissions on this .sh are correct, paths used exist, running
$ps -A | grep -v grep | grep -q node
returns nothing and
$echo $?
1
So shouldn't it be going to the else block? node is a process started after bootup. The shell script does not work correctly both when run by cron or by me when I am SSH'd in.
Am I missing something fundamental here?
TIA
Niko
Another way to check process by name:
Or if shell doesn't support
&>
:This isn't exactly an answer to your question, but perhaps it's a solution to your problem.
One of the issues I've had with Node is that the server process doesn't store its pid anywhere. So rather than having a thing that cron runs to "check" whether it's running, I wrapped the
node
executable in a script that simply relaunches it if it crashes.This script gets run once, when the system starts up. It launches Node, and if Node never crashes, all is well and good, but if it does, I get a notification, a log entry, and a restart.
I run this in FreeBSD, but it should run equally well in Linux or other operating systems.
Unless you're running
BSD ps
, you should be able to use a-C
flag orpgrep
For example,
or