at.*_*at. 1 bash shell process .bash-profile
我想确保node在登录时正在运行。所以在我的.bashrc文件中我有:
pkill node
sleep 1
node server.js &
Run Code Online (Sandbox Code Playgroud)
当然,这不会检查是否node正在运行......它只是杀死它并重新启动它。相反,我想要这样的东西:
node_process = $(pidof node)
if [not_exists node_process]; then
node server.js &
fi
Run Code Online (Sandbox Code Playgroud)
问题是该not_exists方法似乎不存在:)。如何在 Bash 中测试数字或字符串是否存在,这是确保节点在登录时启动并运行的最佳方法吗?
您可以使用以下命令检查字符串是否为空-z:
node_process_id=$(pidof node)
if [[ -z $node_process_id ]]; then
node server.js &
fi
Run Code Online (Sandbox Code Playgroud)
pidof如果找不到匹配的进程,则不返回任何内容,因此node_process_id将设置为空字符串。