ÔõÑùͨ¹ýSystemdºÍCrontabÔÚLinuxϵͳÖÐÖÎÀíϵͳЧÀÍ
ÔõÑùͨ¹ýSystemdºÍCrontabÔÚLinuxϵͳÖÐÖÎÀíϵͳЧÀÍ
ÔÚLinuxϵͳÖУ¬SystemdÊÇÒ»ÖÖ³£¼ûµÄЧÀÍÖÎÀíÆ÷£¬ËüÌṩÁËÒ»ÖÖÀû±ãµÄ·½·¨À´ÖÎÀíϵͳЧÀÍ¡£¶øCrontabÔòÊÇÒ»ÖÖÓÃÓÚ°´ÆÚÖ´ÐÐʹÃüµÄ¹¤¾ß¡£±¾ÎĽ«ÏêϸÏÈÈÝÔõÑùʹÓÃSystemdºÍCrontabÀ´ÖÎÀíϵͳЧÀÍ£¬²¢ÌṩÏêϸ´úÂëʾÀý¡£
Ò»¡¢SystemdµÄʹÓÃ
½¨ÉèЧÀ͵¥Î»
ÔÚSystemdÖУ¬Ã¿¸öϵͳЧÀͶ¼ÐèÒªÒ»¸ö¶ÔÓ¦µÄЧÀ͵¥Î»Îļþ¡£Í¨³££¬ÕâЩÎļþλÓÚ/etc/systemd/systemĿ¼Ï£¬ÃüÃû¹æÔòΪ”ЧÀÍÃû.service”¡£ÏÂÃæÊÇÒ»¸öʾÀýµÄЧÀ͵¥Î»Îļþ£º
[Unit] Description=MyService After=network.target [Service] ExecStart=/path/to/my-service.sh Type=simple Restart=always [Install] WantedBy=default.target
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬Description×Ö¶ÎÌåÏÖЧÀ͵ÄÐÎò£¬After×Ö¶ÎÌåÏÖÔÚÄĸöÄ¿µÄÖ®ºóÆô¶¯Ð§ÀÍ£¬ExecStart×Ö¶ÎÖ¸¶¨Ð§ÀÍÆô¶¯Ê±ÒªÖ´ÐеÄÏÂÁType×Ö¶ÎÖ¸¶¨Ð§À͵ÄÀàÐÍ£¬Restart×Ö¶ÎÖ¸¶¨Ð§ÀÍ×èֹʱÊÇ·ñÖØÐÂÆô¶¯£¬WantedBy×Ö¶ÎÖ¸¶¨Ð§ÀÍËùÊôµÄÄ¿µÄ¡£
Æô¶¯¡¢×èÖ¹ºÍÖØÆôЧÀÍ
ͨ¹ýSystemd£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´Æô¶¯¡¢×èÖ¹ºÍÖØÆôЧÀÍ£º
systemctl start ЧÀÍÃû systemctl stop ЧÀÍÃû systemctl restart ЧÀÍÃû
µÇ¼ºó¸´ÖÆ
Éó²éЧÀÍ״̬
ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÉó²éЧÀ͵ÄÔËÐÐ״̬£º
systemctl status ЧÀÍÃû
µÇ¼ºó¸´ÖÆ
ÉèÖÃЧÀÍ¿ª»ú×ÔÆô¶¯
Òª½«Ð§ÀÍÉèÖÃΪ¿ª»ú×ÔÆô¶¯£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ
systemctl enable ЧÀÍÃû
µÇ¼ºó¸´ÖÆ
Éó²éЧÀÍÈÕÖ¾
ͨ¹ýÒÔÏÂÏÂÁÎÒÃÇ¿ÉÒÔÉó²éЧÀ͵ÄÈÕÖ¾£º
journalctl -u ЧÀÍÃû
µÇ¼ºó¸´ÖÆ
¶þ¡¢CrontabµÄʹÓÃ
±à¼CrontabÎļþ
Òª±à¼CrontabÎļþ£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ
crontab -e
µÇ¼ºó¸´ÖÆ
ÉèÖÃ׼ʱʹÃü
ÔÚ·¿ªµÄ±à¼Æ÷ÖÐÌí¼Ó׼ʱʹÃüµÄÉèÖã¬ÒÔÏÂÊÇÒ»¸öʾÀý£º
# ÌìÌìÆÆÏþ1µãÖ´Ðо籾 0 1 * * * /path/to/my-script.sh # ÿÖÜÒ»ÆÆÏþ2µãÖ´Ðо籾 0 2 * * 1 /path/to/my-other-script.sh
µÇ¼ºó¸´ÖÆ
ÉÏÊöʾÀýÖеÄÉèÖÃÌåÏÖÌìÌìÆÆÏþ1µãºÍÿÖÜÒ»ÆÆÏþ2µã»®·ÖÖ´ÐжÔÓ¦µÄ¾ç±¾¡£
Éó²é׼ʱʹÃü
ÒªÉó²éÄ¿½ñÓû§µÄ׼ʱʹÃüÁÐ±í£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ
crontab -l
µÇ¼ºó¸´ÖÆ
ɾ³ý׼ʱʹÃü
Ҫɾ³ýÄ¿½ñÓû§µÄ׼ʱʹÃü£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ
crontab -r
µÇ¼ºó¸´ÖÆ
Èý¡¢SystemdÓëCrontabµÄÍŽáÓ¦ÓÃ
SystemdºÍCrontab¿ÉÒÔÍŽáʹÓã¬ÒÔ±ã¸üºÃµØÖÎÀíϵͳЧÀÍ¡£ÒÔÏÂÊÇÒ»¸öʾÀý£º
Ê×ÏÈ£¬ÎÒÃÇ¿ÉÒÔ½¨ÉèÒ»¸öЧÀ͵¥Î»Îļþ£¬ÈçÉÏÃæµÄʾÀýËùʾ£¬¸ÃЧÀ͵¥Î»Ö´ÐÐÒ»¸ö¾ç±¾¡£
È»ºó£¬ÔÚCrontabÎļþÖÐÉèÖÃ׼ʱʹÃü£¬ÒÔÌìÌìÆÆÏþ3µãÖØÆô¸ÃЧÀÍ£º
0 3 * * * systemctl restart ЧÀÍÃû
µÇ¼ºó¸´ÖÆ
ͨ¹ýÒÔÉÏÉèÖã¬ÏµÍ³»áÔÚÌìÌìÆÆÏþ3µãÖØÆô¶ÔÓ¦µÄЧÀÍ¡£
×ܽá
ͨ¹ýSystemdºÍCrontab£¬ÎÒÃÇ¿ÉÒÔÀû±ãµØÖÎÀíϵͳЧÀͺÍ׼ʱʹÃü¡£Í¨¹ý½¨ÉèÊʵ±µÄЧÀ͵¥Î»ÎļþºÍÉèÖúÏÊʵÄ׼ʱʹÃü£¬ÎÒÃÇÄܹ»ÔÚLinuxϵͳÖÐ×Ô½ç˵ЧÀ͵ÄÆô¶¯¡¢×èÖ¹¡¢ÖØÆô£¬ÒÔ¼°°´ÆÚÖ´Ðо籾µÈ²Ù×÷¡£×¼È·Ê¹ÓÃSystemdºÍCrontab£¬Äܹ»×ÊÖúÎÒÃǸüºÃµØÖÎÀíϵͳ£¬²¢Ìá¸ßϵͳµÄÎȹÌÐԺͿɿ¿ÐÔ¡£
ÒÔÉϾÍÊÇÔõÑùͨ¹ýSystemdºÍCrontabÔÚLinuxϵͳÖÐÖÎÀíϵͳЧÀ͵ÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡