2

The get_pid() function below is intended to return the PID of daemon_itinerary.sh.

The script below is not in the same working directory as daemon_itinerary.sh.

#!/bin/bash

PID=""

get_pid() {
    PID='pidof daemon_itinerary.sh'
}

start() {
    echo "Restarting test_daemon"
    get_pid
    if [[ -z $PID ]]; then
        echo "starting test_daemon .."
        sh /var/www/bin/daemon_itinerary.sh &
        get_pid
        echo "done. PID=$PID"
    else
        echo "test_deamon is alrady running, PID=$PID"
    fi
}

case "$1" in
start)
    start
;;
...
*)
    echo "Usage: $0 {start|stop|restart|status}"
esac

*edit

Start is being passed in as a command line argument.

6
  • So, what's the question? :-| Commented Jan 24, 2018 at 1:06
  • Aren't you calling start() from anywhere?? Commented Jan 24, 2018 at 1:15
  • pidof daemon_itinerary.sh should be in backticks, not single quotes. Commented Jan 24, 2018 at 1:17
  • Make daemon_itinerary.sh executable (700) and call directly, not with 'sh'. Then use 'pidof -x daemon_itinerary.sh' Commented Jan 24, 2018 at 1:37
  • 2
    not really, as Roadawl suggested yous should use pidof -x daemon_itinerary.sh. To capture the PID in a variable it should be pid=$(pidof daemon_itinerary.sh). Please note the lower case on pid variable as PID is a reserved name in bash. Commented Jan 24, 2018 at 2:23

1 Answer 1

4

we use pgrep to get the pid of a processm like below

PID=$(pgrep -f "daemon_itinerary.sh" | xargs)

# xargs - is given because pgrep will return both process id as well as parent pid
# also it will help us to get all pids if multiple instances are running.
# pgrep option to get session id or parent id alone, here its from manual
# -P, --parent ppid,...
#     Only match processes whose parent process ID is listed.
# -s, --session sid,...
#     Only match processes whose process session ID is listed. Session ID 0 is translated into pgrep's or pkill's own session ID.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.