ÔõÑùÔÚLinuxϵͳÖÐʹÓÃSystemdºÍCrontabʵÏÖʹÃüµÄ´®ÐÐÖ´ÐÐ
ÔõÑùÔÚLinuxϵͳÖÐʹÓÃSystemdºÍCrontabʵÏÖʹÃüµÄ´®ÐÐÖ´ÐÐ
ÔÚLinuxϵͳÖУ¬SystemdÊÇÒ»¸öϵͳºÍЧÀÍÖÎÀíÆ÷£¬¶øCrontabÊÇÒ»¸öÓÃÓÚÖ´ÐÐÍýÏëʹÃüµÄ¹¤¾ß¡£ÍŽáʹÓÃÕâÁ½¸ö¹¤¾ß£¬ÎÒÃÇ¿ÉÒÔʵÏÖʹÃüµÄ´®ÐÐÖ´ÐС£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚLinuxϵͳÖÐʹÓÃSystemdºÍCrontabʵÏÖʹÃüµÄ´®ÐÐÖ´ÐУ¬²¢ÌṩÏêϸµÄ´úÂëʾÀý¡£
SystemdÊÇLinuxϵͳÖеÄÒ»¸öÖ÷ÒªµÄЧÀÍÖÎÀíÆ÷£¬Ëü¿ÉÒÔÆô¶¯¡¢×èÖ¹¡¢ÖØÆôºÍÖÎÀíϵͳµÄÖÖÖÖЧÀÍ¡£Í¨¹ý½¨ÉèSystemdµ¥Î»Îļþ£¬ÎÒÃÇ¿ÉÒÔ½«Ê¹ÃüÒÔЧÀ͵ÄÐÎʽÔËÐС£¶øCrontabÊÇÒ»¸öÓÃÓÚÍýÏëʹÃüµÄ¹¤¾ß£¬Ëüͨ¹ýÔÚÌض¨µÄʱ¼äµã»ò¾àÀëÄÚÔËÐÐÖ¸¶¨µÄÏÂÁî»ò¾ç±¾¡£ÍŽáʹÓÃÕâÁ½¸ö¹¤¾ß£¬ÎÒÃÇ¿ÉÒÔʵÏÖʹÃüµÄ´®ÐÐÖ´ÐУ¬¼´ÔÚÒ»¸öʹÃüÍê³ÉºóÔÙÖ´ÐÐÏÂÒ»¸öʹÃü¡£
ÒÔÏÂÊÇÏêϸµÄ°ì·¨ºÍ´úÂëʾÀý£º
µÚÒ»²½£º½¨ÉèSystemdµ¥Î»Îļþ
½¨ÉèÒ»¸öеÄSystemdµ¥Î»Îļþ£¬ÀýÈçtask1.service£¬²¢ÉúÑÄÔÚ/etc/systemd/system/Ŀ¼Ï¡£
ÔÚµ¥Î»ÎļþÖУ¬±àдʹÃü1µÄÐÎòÐÅÏ¢ºÍÖ´ÐÐÏÂÁî¡£ÀýÈ磺
[Unit] Description=Task1 [Service] ExecStart=/path/to/task1.sh
µÇ¼ºó¸´ÖÆ
µÚ¶þ²½£º½¨ÉèCrontab׼ʱʹÃü
·¿ªCrontabÉèÖÃÎļþ£¬ÔËÐÐÏÂÁcrontab -e¡£
ÔÚÎļþÖУ¬Ìí¼ÓÒ»¸öеÄ׼ʱʹÃü£¬ÀýÈçtask2£¬Ö¸¶¨Ö´ÐÐʱ¼äºÍÒªÖ´ÐеÄÏÂÁî¡£ÀýÈ磺
0 1 * * * /path/to/task2.sh
µÇ¼ºó¸´ÖÆ
µÚÈý²½£ºÉèÖÃSystemdµ¥Î»ÎļþÒÀÀµ¹Øϵ
ÐÞ¸Ätask2.sh¾ç±¾£¬ÔÚʹÃü2Ö´ÐÐ֮ǰÆÚ´ýʹÃü1Íê³É¡£¿ÉÒÔʹÓÃSystemdµÄsystemctlÏÂÁîÀ´ÆÚ´ýʹÃü1Íê³É¡£ÀýÈ磺
#!/bin/bash systemctl start task1.service systemctl status task1.service # ÆÚ´ýʹÃü1Íê³É£¬¿ÉÒÔƾ֤ʹÃü1µÄÈÕÖ¾»ò״̬À´ÅÐ¶Ï while [[ $(systemctl is-active task1.service) == "active" ]]; do sleep 1 done # Ö´ÐÐʹÃü2µÄÂß¼
µÇ¼ºó¸´ÖÆ
µÚËIJ½£ºÖØÆôSystemdºÍCrontabЧÀÍ
ÔËÐÐÒÔÏÂÏÂÁîÖØÆôSystemdЧÀÍ£¬²¢Ê¹ÉèÖÃÎļþÉúЧ£º
systemctl daemon-reload systemctl restart task1.service
µÇ¼ºó¸´ÖÆ
ÔËÐÐÒÔÏÂÏÂÁîÖØÆôCrontabЧÀÍ£¬²¢Ê¹ÉèÖÃÎļþÉúЧ£º
systemctl restart cron.service
µÇ¼ºó¸´ÖÆ
ͨ¹ýÒÔÉÏ°ì·¨ºÍʾÀý´úÂ룬ÎÒÃÇ¿ÉÒÔʵÏÖʹÃüµÄ´®ÐÐÖ´ÐС£µ±Crontab׼ʱʹÃü´¥·¢Ê±£¬Systemdµ¥Î»Îļþ»á±»Æô¶¯²¢×îÏÈÖ´ÐÐʹÃü1¡£ÔÚʹÃü1Íê³Éºó£¬Ê¹Ãü2¾ç±¾»áÆÚ´ýʹÃü1µÄ״̬±äΪ·ÇÔ˶¯×´Ì¬£¬È»ºóÔÙÖ´ÐÐʹÃü2µÄÂß¼¡£
×¢ÖØÊÂÏ
ÔÚ±àдSystemdµ¥Î»ÎļþºÍ¾ç±¾Ê±£¬Ó¦È·±£¾ç±¾µÄ·¾¶×¼È·£¬²¢ÉèÖÃ׼ȷµÄȨÏÞ¡£
ÔÚÉèÖÃCrontab׼ʱʹÃüʱ£¬Ó¦ÔÚCrontabÉèÖÃÎļþÖÐÖ¸¶¨×¼È·µÄʱ¼äºÍÏÂÁî¡£
ÔÚÉèÖÃSystemdµ¥Î»Îļþʱ£¬¿ÉÒÔÉèÖÃÆäËûµÄÒÀÀµ¹Øϵ¡¢ÇéÐαäÁ¿µÈ¡£
×ÛÉÏËùÊö£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÍŽáʹÓÃSystemdºÍCrontab£¬ÔÚLinuxϵͳÖÐʵÏÖʹÃüµÄ´®ÐÐÖ´ÐС£Í¨¹ýSystemdµ¥Î»ÎļþºÍCrontab׼ʱʹÃüµÄÉèÖã¬ÎÒÃÇ¿ÉÒÔÎÞаµØʵÏÖ¶à¸öʹÃüµÄ´®ÐÐÖ´ÐУ¬ÒÔÖª×ã²î±ðµÄÐèÇó¡£
ÒÔÉϾÍÊÇÔõÑùÔÚLinuxϵͳÖÐʹÓÃSystemdºÍCrontabʵÏÖʹÃüµÄ´®ÐÐÖ´ÐеÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡