
我需要在Suse Linux中實作一個應用程式根據系統時間自動啟動和關閉。例如。上午 10 點開始申請。下午 5 點關閉應用程式。這可以使用 shell 腳本來完成嗎?
答案1
您可以使用 cron 來完成相同的任務
如果你想編輯 cron 作業只需使用crontab -e
指令。它將開啟export EDITOR=vim
帶有已定義的 cronjobs 的首選 ( ) 編輯器。然後輸入如下:
# crontab fields
# <minute> <hour> <day of month> <month> <day of week> <command>
# Start the Application at 10am
00 10 * * * /path/to/startapp_script >/dev/null 2>&1
# Stop the Application at 5pm
00 17 * * * /path/to/stopapp_script >/dev/null 2>&1
要了解有關 cron 的更多信息,請參閱這頁。
如果您的應用程式沒有啟動/停止初始化腳本,那麼您可以建立自己的自訂初始化腳本,請參閱以下連結。
答案2
您可以使用 2 個 cron 作業,一個在上午 10 點啟動應用程序,另一個在下午 5 點停止它。
00 10 * * * /path/to/start-script.sh
00 17 * * * /path/to/stop-script.sh
如果您希望它們僅在周一至週五運行,則如下所示:
00 10 * * Mon-Fri /path/to/start-script.sh
00 17 * * Mon-Fri /path/to/stop-script.sh