Shell-Script

Cron 作業立即終止會話

  • August 18, 2021

我在 cronjob 中有這個條目:

$ cronjob -l
* * * * * $(cd /home/fedor/Documents/mecab_natto && bash run_mecab.sh >> /home/fedor/Documents/mecab_natto/log.txt)

當我手動執行腳本時,系統中的任何地方都可以正常執行。但是,當我將它作為 cron 作業執行時,該作業會啟動,但會立即終止 -因此終止我的 sinatra 應用程序

$ journalctl -u cron.service | tail
Aug 18 09:37:01 fedor-desktop CRON[12180]: pam_unix(cron:session): session closed for user fedor
Aug 18 09:38:01 fedor-desktop CRON[12241]: pam_unix(cron:session): session opened for user fedor by (uid=0)
Aug 18 09:38:01 fedor-desktop CRON[12242]: (fedor) CMD ($(cd /home/fedor/Documents/mecab_natto && bash run_mecab.sh >> /home/fedor/Documents/mecab_natto/log.txt))
Aug 18 09:38:01 fedor-desktop CRON[12241]: (CRON) info (No MTA installed, discarding output)
Aug 18 09:38:01 fedor-desktop CRON[12241]: pam_unix(cron:session): session closed for user fedor
Aug 18 09:39:01 fedor-desktop cron[3820]: (fedor) RELOAD (crontabs/fedor)
Aug 18 09:39:01 fedor-desktop CRON[12397]: pam_unix(cron:session): session opened for user fedor by (uid=0)
Aug 18 09:39:01 fedor-desktop CRON[12398]: (fedor) CMD (cd /home/fedor/Documents/mecab_natto && bash run_mecab.sh >> /home/fedor/Documents/mecab_natto/log.txt)
Aug 18 09:39:01 fedor-desktop CRON[12397]: (CRON) info (No MTA installed, discarding output)
Aug 18 09:39:01 fedor-desktop CRON[12397]: pam_unix(cron:session): session closed for user fedor

run_mecab.sh:

. app.config
echo $PID

if [ -n "$(ps -p $PID -o pid=)" ] && [ "$(ps -q $PID -o comm=)" == "ruby" ]; then
 echo "process is running"
 echo $PID
else
 echo "process is not running, starting"
 bundle exec ruby main.rb -o 0.0.0.0 -p 4568 & 
 ID="$!"
 echo $ID
 echo "PID=$ID" > app.config
fi

基本上腳本只是檢查具有 id 的服務是否正在執行,如果沒有,它會啟動一個並保存新的 id。

我在 rpi raspbian上進行了此設置,它執行良好,但在jetson nano 預設圖像(ubuntu 18.something)上,工作開始但立即終止。我不確定從哪裡開始調試,我run_mecab.sh根據另一個 cron 問題調整了權限,但沒有找到任何令人滿意的東西來pam_unix(cron:session): session opened/closed幫助我了解問題可能出在哪裡。

感謝幫助。

添加#!/bin/bash到頂部run_mecab.sh並呼叫:

* * * * * $(cd /home/fedor/Documents/mecab_natto && ./run_mecab.sh >> /home/fedor/Documents/mecab_natto/log.txt)

代替

* * * * * $(cd /home/fedor/Documents/mecab_natto && bash run_mecab.sh >> /home/fedor/Documents/mecab_natto/log.txt)

解決了我的問題。謝謝大家的幫助。

引用自:https://unix.stackexchange.com/questions/665149