我有以下需要在 shell 脚本中运行的命令,
nohup command >> help.out &
当我从终端运行脚本时,nohup
命令在后台运行,并且执行下一个命令,但日志不会写入 help.out 我已经检查了文件 help.out 的权限,它们在脚本中创建为只读,但我使用 chmod -R 777 help.out 更改了权限,并且不再是只读的,但仍然没有任何内容写入 help.out。
我还想知道如何在脚本中创建文件或文件夹,以便它永远不会只读并具有所有权限。
#!/bin/bash
trainingState=1
epoch=50
#URL myspace test
URL="xxxxxx"
nohup python3.6 <arguments> >> help.out &
#processId of xyz
processId=$(pidof python3.6)
#this command executes
curl -X POST -H "Content-Type: application/json" -d '{"markdown" : "### The Training has started !! \n > EPOCS:'"$epoch"'"}' $URL
#while loop also executes but no data to read from file
while [[ $trainingState == 1 ]]; do
if ps -p $processId > /dev/null ; then
echo "training happening"
value=$(tail -n 1 help.out)
curl requests etc .....
else
value=$(tail -n 1 help.out)
echo "training finished"
final curl requests etc .....
trainingState=0
fi
done
答案1
您在后台有进程,并且希望同时将输出重定向到日志文件。您必须按如下方式执行此操作:首先将 stdout 发送到您想要的位置,然后将 stderr 发送到 stdout 所在的地址:
some_cmd > some_file 2>&1 &
您的代码应修改如下:
#!/bin/bash
trainingState=1
epoch=50
#URL myspace test
URL="xxxxxx"
nohup python3.6 <arguments> >> help.out 2>&1 &
#processId of xyz
processId=$(pidof python3.6)
#this command executes
curl -X POST -H "Content-Type: application/json" -d '{"markdown" : "### The Training has started !! \n > EPOCS:'"$epoch"'"}' $URL
#while loop also executes but no data to read from file
while [[ $trainingState == 1 ]]; do
if ps -p $processId > /dev/null ; then
echo "training happening"
value=$(tail -n 1 help.out)
curl requests etc .....
else
value=$(tail -n 1 help.out)
echo "training finished"
final curl requests etc .....
trainingState=0
fi
done