【问题标题】:can shell script make itself run in background after running some steps?运行一些步骤后,shell 脚本可以让自己在后台运行吗?
【发布时间】:2017-07-03 16:34:21
【问题描述】:

我有基于 BBB 的定制嵌入式 Linux 板,带有 busybox shell(ash)

我的脚本必须在后台运行,并满足以下条件

  1. 脚本必须只有一个实例。
  2. 包装脚本需要知道脚本是否在后台成功启动。

还有另一个包装器脚本可以启动和停止我的脚本,包装器脚本如下所述。

#!/bin/sh

export PATH=/bin:/sbin:/usr/bin:/usr/sbin

readonly TEST_SCRIPT_PATH="/home/testscript.sh"
readonly TEST_SCRIPT_LOCK_PATH="/var/run/${TEST_SCRIPT_PATH##*/}.lock"

start_test_script()
{
    local pid_of_testscript=0
    local status=0
    #Run test script in background
    "${TEST_SCRIPT_PATH}" &

    #---------Now When this point is hit, lock file must be created.-----

    if [ -f "${TEST_SCRIPT_LOCK_PATH}" ];then
        pid_of_testscript=$(head -n1 ${TEST_SCRIPT_LOCK_PATH})
        if [ -n "${pid_of_testscript}" ];then
            kill -0 ${pid_of_testscript} &> /dev/null || status="${?}"
            if [ ${status} -ne 0 ];then
                echo "Error starting testscript"
            else
                echo "testscript start successfully"
            fi
        else
            echo "Error starting testscript.sh"
        fi
    fi

}

stop_test_script()
{
    local pid_of_testscript=0
    local status=0

    if [ -f "${TEST_SCRIPT_LOCK_PATH}" ];then
        pid_of_testscript=$(head -n1 ${TEST_SCRIPT_LOCK_PATH})
        if [ -n "${pid_of_testscript}" ];then
            kill -0 ${pid_of_testscript} &> /dev/null || status="${?}"
            if [ ${status} -ne 0 ];then
                echo "testscript not running"
                rm "${TEST_SCRIPT_LOCK_PATH}"
            else
                #send SIGTERM signal
                kill -SIGTERM "${pid_of_testscript}"
            fi
        fi     
   fi

}

#Script starts from here.
case ${1} in

'start')
    start_test_script
    ;;

'stop')
    stop_test_script
    ;;
*)
    echo "Usage: ${0} [start|stop]"
    exit 1
    ;;
esac

现在实际的脚本“testscript.sh”看起来像这样,

#!/bin/sh
#Filename : testscript.sh

export PATH=/bin:/sbin:/usr/bin:/usr/sbin
set -eu

LOCK_FILE="/var/run/${0##*/}.lock"
FLOCK_CMD="/bin/flock"
FLOCK_ID=200
eval "exec ${FLOCK_ID}>>${LOCK_FILE}"
"${FLOCK_CMD}" -n "${FLOCK_ID}" || exit 0
echo "${$}" > "${LOCK_FILE}"

# >>>>>>>>>>-----Now run the code in background---<<<<<<

handle_sigterm()
{
    # cleanup
    "${FLOCK_CMD}" -u "${FLOCK_ID}"
    if [ -f "${LOCK_FILE}"  ];then
        rm "${LOCK_FILE}"
    fi
}
trap handle_sigterm SIGTERM

while true
do
    echo "do something"
    sleep 10
done

现在在上面的脚本中,您可以看到“---现在在后台运行代码--”,此时我确定锁定文件已成功创建,或者该脚本的实例已经在运行。因此,我可以在后台安全地运行其他代码,并且包装脚本可以检查锁定文件并找出锁定文件中提到的进程是否正在运行。 shellscript 本身可以让它在后台运行吗?

如果没有,是否有更好的方法来满足所有条件?

【问题讨论】:

    标签: linux bash shell embedded-linux busybox


    【解决方案1】:

    我认为您可以查看内置的作业控制,特别是 bg

    Job Control Commands

    【讨论】:

      【解决方案2】:

      当进程说它们自己后台时,它们实际上所做的就是分叉并退出父进程。您可以通过使用&amp; 运行任何您想要的命令、函数或语句然后退出来执行相同的操作。

      #!/bin/sh
      echo "This runs in the foreground"
      sleep 3
      
      while true
      do
          sleep 10
          echo "doing background things"
      done & 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-09
        • 2021-08-11
        相关资源
        最近更新 更多