0

我得到一段 PID 文件控制的代码。

程序员的风格,看不懂。。

我不知道-->

&& 的使用

[[ $mypid -ne $procpid ]] **&&**

并正确地重新启动自己(在 MacosX 上不起作用)

$0 $@ &

代码完成...

function createpidfile() {
  mypid=$1
  pidfile=$2
  #Close stderr, don't overwrite existing file, shove my pid in the lock file.
  $(exec 2>&-; set -o noclobber; echo "$mypid" > "$pidfile") 
  [[ ! -f "$pidfile" ]] && exit #Lock file creation failed
  procpid=$(<"$pidfile")
  [[ $mypid -ne $procpid ]] && {
    #I'm not the pid in the lock file
    # Is the process pid in the lockfile still running?
    isrunning "$pidfile" || {
      # No.  Kill the pidfile and relaunch ourselves properly.
      rm "$pidfile"
      $0 $@ &
    }
    exit
  }
}

我迷路了

4

4 回答 4

1

[[ ! -f "$pidfile" ]] && exit表示“如果没有名为 $pidfile 的文件,则退出”(使用短路评估) -exit如果文件存在,则不会被评估。

$0 $@ &

  • $0- 命令行中的第一个参数(表示可执行文件本身);
  • $@- 所有剩余的参数都传递到命令行;
  • &- 启动后将进程发送到后台。
于 2012-04-19T14:16:38.127 回答
1
    command1 && command2

当且仅当 command1 返回退出状态为零时,才会执行 command2。

$0是实际二进制文件的名称。

$@都是参数。

并且关闭&将过程发送到后台。

一切都记录在bash 手册中参见例如部分3.4.2 Special Parameters

于 2012-04-19T14:16:42.997 回答
1

这是布尔短路- 如果&&(and) 运算符之前的位计算结果是,false则不需要执行第二部分(和之间的块{。运算符}使用相同的技巧||,它只会在以下情况下执行第二个块第一个块返回false

于 2012-04-19T14:19:23.270 回答
1
  1. &&是一个逻辑AND

    如果条件[[ $mypid -ne $procpid ]]为真,则块中的代码将{...}被执行。

  2. $0 $@ &在后台重新启动脚本(使用相同的参数)。

    • $0是调用脚本的命令

    • $@是传递给脚本的所有参数的列表

    • &表示前面的命令应该在后台执行

于 2012-04-19T14:17:54.683 回答