如何在不重新啟動的情況下啟動 cron 作業?

如何在不重新啟動的情況下啟動 cron 作業?

我使用 cron 作業offlineimap每 2 分鐘呼叫一次:

*/2 * * * * /usr/bin/offlineimap > ~/Maildir/offlineimap.log 2>&1

我需要終止 cron 作業來解決問題。我怎麼能重新啟動 cron 作業(無需重新啟動)?我在網路上找到了這個「解決方案」:

mylogin@myhost:~$ sudo /etc/init.d/cron restart
Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service cron restart

Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the stop(8) and then start(8) utilities,
e.g. stop cron ; start cron. The restart(8) utility is also available.
cron stop/waiting
cron start/running, process 26958

但是,使用 時ps -ef | grep ...,我看不到該工作...出了什麼問題?

答案1

克朗方法

如果您有 sudo 權限,您可以停止/啟動 cron 服務。我相信這就是您在網路上找到的解決方案所解釋的內容。

根據您使用的 Linux 發行版,您可以執行以下命令:

# redhat distros
$ sudo /etc/init.d/crond stop
... do your work ...
$ sudo /etc/init.d/crond start

或執行以下命令:

# Debian/Ubuntu distros
$ sudo service cron stop
... do your work ...
$ sudo service cron start

鎖定文件類型的方法

當您希望 Offlineimap 任務暫停而不運行一段時間時,您也可以在 /tmp 目錄中放置一個「dontrunofflineimap」檔案。

這個過程會像這樣進行。您可以像這樣觸摸 /tmp 中的檔案:

touch /tmp/dontrunofflineimap

cron 作業將會修改如下:

*/2 * * * * [ -f /tmp/dontrunofflineimap ] || /usr/bin/offlineimap > ~/Maildir/offlineimap.log 2>&1

當該檔案存在時,它基本上會阻止offlineimap應用程式運行。當您想要恢復時,只需刪除該/tmp/dontrunofflineimap檔案即可。

答案2

另一個解決方案是編輯 crontab 並註解掉該作業以停用它。這更好一點,因為cron也可以安排其他工作。

以下命令有幫助:

crontab -e

如果是 root 的 crontab 而不是使用者的:

sudo crontab -e

若要註解掉作業,請#在行首新增 。像這樣:

# */2 * * * * /usr/bin/offlineimap > ~/Maildir/offlineimap.log 2>&1

答案3

你可以閱讀http://tutscode.com/how-to-use-crontab-in-linux/取得有關 crontab 的更多資訊。

相關內容