I have a java runnable jar which I need to run it if it not running or got killed for some reason so I decided to use crontab for this -
I need to run the crontab as "poppetapp" user so I am logged in as this user and created crontab like this -
*/5 * * * * pgrep -f test_java_10.jar || cd /home/poppetapp && /home/poppetapp/test_java.sh > /home/poppetapp/test_java.out
And my shell script is like this which is in /home/poppetapp/
location -
#!/bin/sh
/usr/home/java -jar /home/poppetapp/test_java_10.jar
My java runnable jar is in the home location for "poppetapp" user which is /home/poppetapp/
.
So my question is - Does this look right? I just need to start my test_java_10.jar
if it is not running or got killed for some reason so I created a crontab which will run every 5 minutes to check whether it is running or not. If it is not running it will execute my shell script to start the test_java_10.jar
process.
But somehow when I make this crontab change, I am seeing lot of test_java_10.jar
process is getting invoked. I think every 5 minutes it is launching another process for this.
UPDATE:-
If I modify my crontab like this -
*/5 * * * * pgrep -f test_java_10.jar || /home/poppetapp/test_java.sh > /home/poppetapp/test_java.out
then I see below mail which doesn't say what's the problem or does it say anything?
& t 35
Message 35:
From poppetapp@machineA Tue Mar 24 17:05:01 2015
X-Original-To: poppetapp
From: root@machineA (Cron Daemon)
To: poppetapp@machineA
Subject: Cron <poppetapp@machineA> pgrep -f test_java_10.jar || /home/poppetapp/test_java.sh > /home/poppetapp/test_java.out
Content-Type: text/plain; charset=ANSI_X3.4-1968
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/poppetapp>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=poppetapp>
Date: Tue, 24 Mar 2015 17:05:01 -0700 (GMT+7)
echo $?
right after running it. Does it return a different value if it does/doesn't match? (Because that's what the||
is doing, it's not looking at the command output). Personally though, I'd be tempted to suggest a lock file rather than a ps test. – Sobrique Mar 24 '15 at 21:41echo $?
? – david Mar 24 '15 at 21:43pgrep
on the command line,echo $?
after doing so. But I think I know the problem -pgrep
returns0
if the process is running (e.g.false
). – Sobrique Mar 24 '15 at 21:48$PATH
, in the cron environment, doesn't include what is needed to run Java programs. Perhaps append 2>&1 to your script call, so that the error output also ends up in the log file. Or wrap the entire command in parentheses, and append>/tmp/cron.command.log 2>&1
so we see the error output of the entire thing. – dhag Mar 25 '15 at 00:42pgrep -f test_java_10.jar || (/home/poppetapp/test_java.sh > /home/poppetapp/test_java.out) > /tmp/cron.command.log 2>&1
– david Mar 25 '15 at 00:48(pgrep -f test_java_10.jar || /home/poppetapp/test_java.sh > /home/poppetapp/test_java.out) >/tmp/cron.command.log 2>&1
. – dhag Mar 25 '15 at 01:281445 1447
.. These are the PID? – david Mar 25 '15 at 04:413890 3893
in the same file. – david Mar 25 '15 at 04:46