2

I am new to unix/linux and need a shell script to start a program if it is not running. The program's name is "literaail" and it has a log located "/local/publish/mtc_preprint/util/Literail.log" on a server. This program is supposed to print this "Starting ftp check for Get_Job1." statement once every 30 seconds. If it doesn't, I need a shell script to start the process. Currently I am doing it manually using these commands:

ps -ef | grep ftp_util_mtc_pp.sh
ps -ef | grep literail ( this command lists the jar file related to the literail program)
kill "parentjobnumber" "childjobnumber"
./ftp_util_mtc_pp.sh & ( this command restarts the process ).

I have a "ftp_util_mtc_pp.sh" file, inside this file, i have the following commands:

#!/bin/bash

utildir="/local/publish/mtc_preprint/util"

cd $utildir

java -jar LiteRail_3.0.1.jar literail.properties > /dev/null 2>&1



exit

Can someone help me with the shell-script. Thank you.

Newbee
  • 807
  • 3
  • 14
  • 33
  • have tried to find the process number if exist : PROCESS_NUM='ps -ef | grep "$1" | grep -v "literail" | wc -l' if its not then run java – Jay Ehsaniara Nov 21 '14 at 13:44
  • @MoeEhsani I do not know, what literail is, but keep in mind that other users processes may get counted by this. – lx42.de Nov 21 '14 at 14:33
  • The question as a whole is somewhat misguided -- using a shell script to poll the process table, itself, is not the best way to ensure that a process or service stays running... and yet is assumed by the question as asked. – Charles Duffy Nov 21 '14 at 16:43
  • (`cron` has a maximum resolution of one minutes -- meaning you're getting up to 60 seconds of downtime, and eating all the overhead of starting new programs 60 times an hour for the privilege. Using a real process supervision system, you get zero overhead -- the kernel notifies the supervisor if its children die, so there's no need to periodically check -- and also zero latency, because the notification is triggered by the process exiting, not by a timer; it's a superior approach in every way). – Charles Duffy Nov 21 '14 at 16:44

3 Answers3

2

This file is finding the running LiteRail jar if its running it will kill and rerun it its not just run the the jar and adding the logs in the given location.

ftp_util_mtc_pp.sh is:

#!/bin/bash

PID= ps -ef | grep LiteRail_3

if [[ -z "$PID" ]]; then
        #process is running so we kill it
    Kill -9 PID 
fi

#running the jar file you 
nohup java -jar /local/publish/mtc_preprint/util/LiteRail_3.0.1.jar literail.properties > /local/publish/mtc_preprint/util/Literail.log 2>&1 &

hope this one helps.

Jay Ehsaniara
  • 1,421
  • 17
  • 24
0

There are two common ways to do this:

You save the pid of your process and save it to a file.

#!/bin/bash

pf='/home/abc/pidfile'
if kill -0 $(< "$pf"); then # process in pidfile is yours and running
    exit 0
else
    echo "starting process"
    echo $$ > "$pf"
    exec ftp_util_mtc_pp.sh
fi

Another way would be to use ps and grep to check for the running process:

#!/bin/bash

if ps -u $UID | grep 'LiteRail_3.0.1.jar' >/dev/null 2>&1; then # process is running
    exit 0
else
    echo "starting process"
    exec ftp_util_mtc_pp.sh
fi

Please make sure, the process is detected correctly by the ps | grep line.

If you have pgrep installed:

pgrep -fl -U $UID 'LiteRail_3.0.1.jar'
lx42.de
  • 418
  • 2
  • 4
0

Instead of:

ps -ef | grep ftp_util_mtc_pp.sh

Use:

pgrep ftp_util_mtc_pp.sh

That will avoid the problem of you picking up your own grep command as a process. You can also specify a particular user:

pgrep -u $user ftp_util_mtc_pp.sh

However, since your shell script is mainly running a jarfile, you should look for that:

pgrep -u $user -f "LiteRail_.*\.jar"

The -f says to look for the regular expression in the entire command string.

Now we have that out of the way, let's talk about crontabs. A crontab is a command that you can specify how often you want it to run. You place the command in your crontab and it can run as often as once per minute.

You could create a script to check to see if your java command is running, and if it isn't restart the command. Standard output is mailed to your, so you may want to redirect it to a log file, or to /dev/null.

Standard operating procedure is to capture the process id of the process you want to make sure is running, and then just check to see if that process id is still up and running. If not, restart the job. You can capture the process ID by looking at the $! environment variable after you start your job and toss it into the background.

David W.
  • 105,218
  • 39
  • 216
  • 337