Check running status of a background process launched from same bash script

parthasarathy

I have to write a bash script that launches a process in background in accordance to command line argument passed and returns if it were successfully able to run launch the program.

Here is a pseudo code of what I am trying to achieve

if [ "$1" = "PROG_1" ] ; then
    ./launchProg1 &
    if [ isLaunchSuccess ] ; then
        echo "Success"
    else
        echo "failed"
        exit 1
    fi
elif [ "$1" = "PROG_2" ] ; then
    ./launchProg2 &
    if [ isLaunchSuccess ] ; then
        echo "Success"
    else
        echo "failed"
        exit 1
    fi
fi

Script cannot wait or sleep since it will be called by another mission critical c++ program and needs high throughput ( wrt no of processes started per second ) and moreover running time of processes are unknown. Script neither needs to capture any input/output nor waits for launched process' completion.

I have unsuccessfully tried the following:

#Method 1
if [ "$1" = "KP1" ] ; then
    echo "The Arguement is KP1"
    ./kp 'this is text' &
    if [ $? = "0" ] ; then
        echo "Success"
    else
        echo "failed"
        exit 1
    fi
elif [ "$1" = "KP2" ] ; then
    echo "The Arguement is KP2"
    ./NoSuchCommand 'this is text' &
    if [ $? = "0" ] ; then
        echo "Success"
    else
        echo "failed"
        exit 1
    fi
#Method 2
elif [ "$1" = "CD5" ] ; then
    echo "The Arguement is CD5"
    cd "doesNotExist" &
    PROC_ID=$!
    echo "PID is $PROC_ID"
    if kill -0 "$PROC_ID" ; then
        echo "Success"
    else
        echo "failed"
        exit 1
    fi
#Method 3
elif [ "$1" = "CD6" ] ; then
    echo "The Arguement is CD6"
    cd .. &
    PROC_ID=$!
    echo "PID is $PROC_ID"
    ps -eo pid | grep "$PROC_ID" && { echo "Success"; exit 0; }
    ps -eo pid | grep  "$PROC_ID" || { echo "failed" ; exit 1; }
else
    echo "Unknown Argument"
    exit 1
fi

Running the script gives unreliable output. Method 1, 2 always return Success while Method 3 returns failed when process execution finishes before the checks.

Here is sample tested on GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu) and GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)

[scripts]$ ./processStarted3.sh KP1
The Arguement is KP1
Success
[scripts]$ ./processStarted3.sh KP2
The Arguement is KP2
Success
./processStarted3.sh: line 13: ./NoSuchCommand: No such file or directory
[scripts]$ ./processStarted3.sh CD6
The Arguement is CD6
PID is 25050
failed

As suggested in similar questions, I cannot use process names as one process may be executed several times and others can't be applied.

I have not tried screen and tmux, since getting permission to install them on production servers wont be easy ( but will do so if that is the only option left )

UPDATE
@ghoti
./kp is program which exists and launching the program returns Success. ./NoSuchCommand does not exist. Still as you can see from (edited) output, script incorrectly returns Success.

It does not matter when the process completes execution or program abnormally terminates. Programs launched via script are not tracked in any way ( hence we do not store pid in any table nor necessity arises to use deamontools ).

@Etan Reisner
Example of a program which fails to launch will be ./NoSuchCommand,which does not exist. Or maybe a corrupted program which fails to start.

@Vorsprung
Calling a script which launches a program in background does not take alot of time ( and is manageable as per our expectations). But sleep 1 will accumulate over time to cause issues.

Aforementioned #Method3 works fine barring processes which terminate before ps -eo pid | grep "$PROC_ID" && { echo "Success"; exit 0; } check can be performed.

rajenpandit

Here is an example which will show the result of a process whether it is started successfully or not.

#!/bin/bash
$1 & #executes a program in background which is provided as an argument
pid=$! #stores executed process id in pid
count=$(ps -A| grep $pid |wc -l) #check whether process is still running
if [[ $count -eq 0 ]] #if process is already terminated, then there can be two cases, the process executed and stop successfully or it is terminated abnormally
then
        if wait $pid; then #checks if process executed successfully or not
                echo "success"
        else                    #process terminated abnormally
                echo "failed (returned $?)"
        fi
else
        echo "success"  #process is still running
fi

#Note: The above script will only provide a result whether process started successfully or not. If porcess starts successfully and later it terminates abnormally then this sciptwill not provide a correct result

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Alternative bash script to check if a process is running

From Dev

Bash script to check a specific php process is running?

From Dev

Forcing a process to background from bash script

From Dev

How to write a crontab script, that will check a process' status and launch it if not running?

From Dev

Exit from bash script but keep the process running

From Dev

Exit from bash script but keep the process running

From Dev

Run node as background process from script running as child process

From Dev

Run node as background process from script running as child process

From Dev

bash script with background process

From Dev

Check status of running php script

From Dev

Running ssh script with background process

From Dev

Shell script to check if process is running

From Dev

Check if a process is running with shell script

From Dev

How to start/end a background process from PHP/bash script?

From Dev

Running piped bash script in background

From Dev

determine current process priority from within a running bash script

From Dev

check status of multiple rsync running in background

From Dev

PHP Calling shell_exec on a bash script running a background process times out

From Dev

Running a process in the background as part of a longer script

From Dev

Running Python Script as a Windows background process

From Dev

Running a script when background process crashes

From Dev

Chrome Extension: Check if background script is running

From Dev

pidof from a background script for another background process

From Dev

pidof from a background script for another background process

From Dev

Linux Script to check if process is running and act on the result

From Dev

How to check the status of bash shell script while executing from Python script?

From Dev

Bash Script to start "orphan" background process

From Dev

How do I check if a running process is a background process?

From Dev

How do I check if a running process is a background process?

Related Related

  1. 1

    Alternative bash script to check if a process is running

  2. 2

    Bash script to check a specific php process is running?

  3. 3

    Forcing a process to background from bash script

  4. 4

    How to write a crontab script, that will check a process' status and launch it if not running?

  5. 5

    Exit from bash script but keep the process running

  6. 6

    Exit from bash script but keep the process running

  7. 7

    Run node as background process from script running as child process

  8. 8

    Run node as background process from script running as child process

  9. 9

    bash script with background process

  10. 10

    Check status of running php script

  11. 11

    Running ssh script with background process

  12. 12

    Shell script to check if process is running

  13. 13

    Check if a process is running with shell script

  14. 14

    How to start/end a background process from PHP/bash script?

  15. 15

    Running piped bash script in background

  16. 16

    determine current process priority from within a running bash script

  17. 17

    check status of multiple rsync running in background

  18. 18

    PHP Calling shell_exec on a bash script running a background process times out

  19. 19

    Running a process in the background as part of a longer script

  20. 20

    Running Python Script as a Windows background process

  21. 21

    Running a script when background process crashes

  22. 22

    Chrome Extension: Check if background script is running

  23. 23

    pidof from a background script for another background process

  24. 24

    pidof from a background script for another background process

  25. 25

    Linux Script to check if process is running and act on the result

  26. 26

    How to check the status of bash shell script while executing from Python script?

  27. 27

    Bash Script to start "orphan" background process

  28. 28

    How do I check if a running process is a background process?

  29. 29

    How do I check if a running process is a background process?

HotTag

Archive