ÔõÑùÔÚ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ÉÏÉèÖÃϵͳ׼ʱʹÃüµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡