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

ÔõÑùÔÚLinuxÉÏÉèÖÃϵͳ׼ʱʹÃü

ÔõÑùÔÚlinuxÉÏÉèÖÃϵͳ׼ʱʹÃü

ÔÚLinuxϵͳÖУ¬×¼Ê±Ê¹ÃüÊÇÒ»ÖַǾ­³£¼ûºÍÊÊÓõĹ¦Ð§£¬¿ÉÒÔÓÃÀ´°´ÆÚÖ´ÐÐijЩ²Ù×÷£¬ºÃ±È±¸·ÝÊý¾Ý¡¢ÕûÀíÔÝʱÎļþ¡¢°´ÆÚ¸üÐÂÈí¼þµÈ¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÏÈÈÝÔõÑùÔÚLinuxϵͳÖÐÉèÖÃϵͳ׼ʱʹÃü£¬²¢¸½ÉÏÏìÓ¦µÄ´úÂëʾÀý¡£

LinuxϵͳÖÐÓжàÖÖ׼ʱʹÃüµÄʵÏÖ·½·¨£¬ÆäÖÐ×î³£ÓõÄÊÇcronʹÃüºÍsystemd׼ʱÆ÷¡£ÏÂÃæ»®·ÖÏÈÈÝÕâÁ½ÖÖ·½·¨µÄÉèÖÃÒªÁìºÍ´úÂëʾÀý¡£

Ò»¡¢Ê¹ÓÃcronʹÃüÉèÖÃ׼ʱʹÃü

±à¼­cronʹÃü±í

ÔÚÏÂÁîÐÐÖÐÊäÈëÒÔÏÂÏÂÁ¿ÉÒԱ༭Ŀ½ñÓû§µÄcronʹÃü±í£º

crontab -e

µÇ¼ºó¸´ÖÆ

ÈôÊÇÐèÒª±à¼­ÏµÍ³¹æÄ£µÄcronʹÃü±í£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ

sudo crontab -e

µÇ¼ºó¸´ÖÆ

±àдcronʹÃü

ÔÚ·­¿ªµÄÎļþÖУ¬Ã¿Ò»Ðдú±íÒ»¸ö׼ʱʹÃü¡£Ã¿ÐеÄÃûÌÃÈçÏ£º

·ÖÖÓ Ð¡Ê± ÈÕ Ô ÖÜ ÒªÖ´ÐеÄÏÂÁî

µÇ¼ºó¸´ÖÆ

ÆäÖУ¬·ÖÖÓ¡¢Ð¡Ê±¡¢ÈÕ¡¢ÔºÍÖÜÌåÏÖʹÃüÖ´ÐеÄʱ¼ä£¬¿ÉÒÔʹÓÃͨÅä·û*ÌåÏÖí§ÒâÖµ¡£ÒªÖ´ÐеÄÏÂÁîÊÇÐèҪ׼ʱִÐеIJÙ×÷¡£

ÀýÈ磬ÒÔÏÂÊÇÒ»¸öÌìÌìÆÆÏþ1µãÖ´ÐÐÒ»¸ö¾ç±¾µÄ׼ʱʹÃüʾÀý£º

0 1 * * * /path/to/script.sh

µÇ¼ºó¸´ÖÆ

ÉúÑÄcronʹÃü

±à¼­ÍêcronʹÃüºó£¬°´Ctrl +XÉúÑIJ¢Í˳ö¡£ÏµÍ³»á×Ô¶¯½«Ð޸ĺóµÄcronʹÃü±íÉúÑÄÔÚÏìÓ¦µÄλÖá£

ÌáÐÑ£ºÈôÊÇÐèҪɾ³ýij¸öcronʹÃü£¬Ê¹ÓÃÒÔÏÂÏÂÁ

crontab -r

µÇ¼ºó¸´ÖÆ

¶þ¡¢Ê¹ÓÃsystemd׼ʱÆ÷ÉèÖÃ׼ʱʹÃü

½¨ÉèÒ»¸öserviceÎļþ

ÔÚ/etc/systemd/system/Ŀ¼Öн¨ÉèÒ»¸öÒÔ.serviceΪºó׺µÄÎļþ£¬ºÃ±Èmytask.service¡£

sudo nano /etc/systemd/system/mytask.service

µÇ¼ºó¸´ÖÆ

±àдserviceÎļþÄÚÈÝ

ÔÚ½¨ÉèµÄserviceÎļþÖУ¬¼ÓÈëÒÔÏÂÄÚÈÝ£º

[Unit]
Description=My Task

[Service]
ExecStart=/path/to/script.sh

[Install]
WantedBy=multi-user.target

µÇ¼ºó¸´ÖÆ

ÆäÖУ¬ExecStartÌåÏÖÒªÖ´Ðеľ籾Îļþ£¬WantedBy=multi-user.targetÌåÏÖÕâ¸öʹÃüÓ¦¸ÃÔÚϵͳÆô¶¯Ê±Ö´ÐС£

½¨ÉèÒ»¸ötimerÎļþ

ÔÚ/etc/systemd/system/Ŀ¼Öн¨ÉèÒ»¸öÒÔ.timerΪºó׺µÄÎļþ£¬ºÃ±Èmytask.timer¡£

sudo nano /etc/systemd/system/mytask.timer

µÇ¼ºó¸´ÖÆ

±àдtimerÎļþÄÚÈÝ

ÔÚ½¨ÉèµÄtimerÎļþÖУ¬¼ÓÈëÒÔÏÂÄÚÈÝ£º

[Unit]
Description=Run My Task every day

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target

µÇ¼ºó¸´ÖÆ

ÆäÖУ¬OnCalendar=dailyÌåÏÖÕâ¸öʹÃüÌìÌìÖ´ÐÐÒ»´Î£¬Persistent=trueÌåÏÖ×ÝȻϵͳÔÚʹÃüÖ´ÐÐʱ¼ä֮ǰ¹Ø»úÁË£¬Ê¹ÃüÒ²»áÔÚÏ´ÎÆô¶¯Ê±Ö´ÐС£

ÖØÔØsystemdÉèÖÃ

Ö´ÐÐÒÔÏÂÏÂÁîʹsystemdÖØмÓÔØÉèÖÃÎļþ£º

sudo systemctl daemon-reload

µÇ¼ºó¸´ÖÆ

Æô¶¯×¼Ê±Ê¹Ãü

Ö´ÐÐÒÔÏÂÏÂÁîÆô¶¯×¼Ê±Ê¹Ãü£º

sudo systemctl start mytask.timer

µÇ¼ºó¸´ÖÆ

ÉèÖÿª»ú×ÔÆô¶¯

Ö´ÐÐÒÔÏÂÏÂÁîÉèÖÃ׼ʱʹÃü¿ª»ú×ÔÆô¶¯£º

sudo systemctl enable mytask.timer

µÇ¼ºó¸´ÖÆ

ÒÔÉϾÍÊÇÔÚLinuxϵͳÉÏÉèÖÃϵͳ׼ʱʹÃüµÄ°ì·¨ºÍ´úÂëʾÀý¡£Í¨¹ýʹÓÃcronʹÃü»òsystemd׼ʱÆ÷£¬ÎÒÃÇ¿ÉÒÔÀû±ãµØ°´ÆÚÖ´ÐÐÐèÒªµÄ²Ù×÷£¬Ìá¸ßÊÂÇéЧÂʺÍϵͳÖÎÀíµÄ±ã½ÝÐÔ¡£

ÒÔÉϾÍÊÇÔõÑùÔÚLinuxÉÏÉèÖÃϵͳ׼ʱʹÃüµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

QR code
ÍøÕ¾µØͼ