×ðÁú¿­Ê±¹ÙÍøµÇ¼

ÔõÑùʹÓÃSystemdºÍCrontabÔÚLinuxϵͳÖÐʵÏÖʹÃüµÄ²¢ÐÐÖ´ÐÐ

ÔõÑùʹÓÃSystemdºÍCrontabÔÚLinuxϵͳÖÐʵÏÖʹÃüµÄ²¢ÐÐÖ´ÐÐ

ÔÚLinuxϵͳÖÐ £¬Ê¹ÃüµÄ²¢ÐÐÖ´ÐÐÊÇÌá¸ßϵͳЧÂʺÍÐÔÄܵÄÖ÷ÒªÊÖ¶ÎÖ®Ò»¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃSystemdºÍCrontabÁ½¸ö¹¤¾ß £¬ÔÚLinuxϵͳÖÐʵÏÖʹÃüµÄ²¢ÐÐÖ´ÐÐ £¬²¢ÌṩÏêϸµÄ´úÂëʾÀý¡£

Ò»¡¢SystemdÏÈÈÝ

SystemdÊÇÒ»¸öÓÃÓÚÖÎÀíLinuxϵͳÆô¶¯Á÷³ÌºÍЧÀÍÖÎÀíµÄ¹¤¾ß¡£Í¨¹ýÉèÖÃSystemd £¬¿ÉÒÔʵÏÖʹÃüµÄ²¢ÐÐÖ´ÐС£Ïêϸ°ì·¨ÈçÏ£º

±àдSystemdЧÀÍÉèÖÃÎļþ

½¨ÉèÒ»¸öеÄЧÀÍÉèÖÃÎļþ £¬ÀýÈçmytask.service £¬²¢½«ÒÔÏÂÄÚÈÝÌí¼Óµ½ÎļþÖУº

[Unit]
Description=My Task

[Service]
ExecStart=/path/to/mytask.sh      # Ì滻ΪÏÖʵҪִÐеÄʹÃü¾ç±¾Â·¾¶
Type=simple
RemainAfterExit=no

[Install]
WantedBy=multi-user.target

µÇ¼ºó¸´ÖÆ

ÆôÓò¢Æô¶¯SystemdЧÀÍ

ʹÓÃÒÔÏÂÏÂÁîÆôÓò¢Æô¶¯SystemdЧÀÍ£º

sudo cp mytask.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl start mytask.service

µÇ¼ºó¸´ÖÆ

ÕâÑù £¬Ê¹Ãü¾Í»áÔÚºǫ́ÒÔ²¢Ðеķ½·¨Ö´ÐС£

¶þ¡¢CrontabÏÈÈÝ

CrontabÊÇÒ»¸öÓÃÓÚ׼ʱִÐÐʹÃüµÄ¹¤¾ß¡£Í¨¹ýÉèÖÃCrontab £¬¿ÉÒÔʵÏÖʹÃüµÄ²¢ÐÐÖ´ÐС£Ïêϸ°ì·¨ÈçÏ£º

±à¼­CrontabÉèÖÃÎļþ

ʹÓÃÒÔÏÂÏÂÁî±à¼­CrontabÉèÖÃÎļþ£º

crontab -e

µÇ¼ºó¸´ÖÆ

Ìí¼ÓʹÃüµ½Crontab

ÔÚ·­¿ªµÄÉèÖÃÎļþÖÐ £¬Ìí¼ÓÒÔÏÂÄÚÈÝ£º

* * * * * /path/to/mytask.sh     # Ì滻ΪÏÖʵҪִÐеÄʹÃü¾ç±¾Â·¾¶

µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ

ÕâÑù £¬Ê¹Ãü¾Í»áÿ·ÖÖÓÖ´ÐÐÒ»´Î £¬²¢ÐÐÖ´ÐС£

Èý¡¢SystemdºÍCrontabµÄ½ÏÁ¿

SystemdºÍCrontab¶¼¿ÉÒÔʵÏÖʹÃüµÄ²¢ÐÐÖ´ÐÐ £¬µ«ÔÚÓ¦Óó¡¾°ÉÏÓÐËùÇø±ð¡£SystemdÊÊÓÃÓÚÐèÒªÔÚϵͳÆô¶¯Ê±»òÐèÒªÒÔЧÀÍÐÎʽִÐеÄʹÃü £¬¶øCrontabÊÊÓÃÓÚÐèҪ׼ʱִÐеÄʹÃü¡£Æ¾Ö¤ÏÖʵÐèÇóÑ¡ÔñºÏÊʵŤ¾ß¡£

´úÂëʾÀý£º

ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄʹÃü¾ç±¾Ê¾Àýmytask.sh £¬ÊµÏÖÁËÔÚºǫ́´òÓ¡Êý×ֵĹ¦Ð§£º

#!/bin/bash
for i in {1..10}
do
    echo $i
    sleep 1
done

µÇ¼ºó¸´ÖÆ

ʹÓÃSystemdÖ´ÐÐʹÃüµÄ´úÂëʾÀýÒÑÔÚµÚÒ»½ÚÏÈÈÝÖиø³ö¡£

ʹÓÃCrontabÖ´ÐÐʹÃüµÄ´úÂëʾÀý£º

* * * * * /path/to/mytask.sh     # Ì滻ΪÏÖʵҪִÐеÄʹÃü¾ç±¾Â·¾¶

µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ

×¢ÖØÊÂÏ

È·±£Ê¹Ãü¾ç±¾¾ßÓпÉÖ´ÐÐȨÏÞ £¬¿ÉʹÓÃchmod +x mytask.shÏÂÁîΪ¾ç±¾Ìí¼ÓÖ´ÐÐȨÏÞ¡£

È·±£Â·¾¶×¼È· £¬°üÀ¨SystemdÉèÖÃÎļþÖеľ籾·¾¶ºÍCrontabÉèÖÃÎļþÖеľ籾·¾¶¡£

SystemdÉèÖÃÎļþÐèÒª°²ÅÅÔÚ/etc/systemd/system/Ŀ¼Ï¡£

ÔÚʹÓÃCrontabÉèÖÃÎļþʱ £¬×¢ÖØCrontabµÄʱ¼äÃûÌá£

×ܽ᣺

ͨ¹ýʹÓÃSystemdºÍCrontab¹¤¾ß £¬ÎÒÃÇ¿ÉÒÔÔÚLinuxϵͳÖÐʵÏÖʹÃüµÄ²¢ÐÐÖ´ÐС£Í¨¹ýºÏÀíÉèÖú͵÷ÀíʹÃü £¬¿ÉÒÔÌá¸ßϵͳµÄЧÂʺÍÐÔÄÜ¡£Çëƾ֤±¾ÎÄÌṩµÄ°ì·¨ºÍ´úÂëʾÀý¾ÙÐÐÉèÖà £¬Æ¾Ö¤ÏÖʵÐèÇóÑ¡ÔñºÏÊʵŤ¾ßÀ´ÊµÏÖʹÃüµÄ²¢ÐÐÖ´ÐС£

ÒÔÉϾÍÊÇÔõÑùʹÓÃSystemdºÍCrontabÔÚLinuxϵͳÖÐʵÏÖʹÃüµÄ²¢ÐÐÖ´ÐеÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ