Linux Systemd Crontab½Ì³Ì£ºÑ§Ï°ÔõÑù×Ô¶¯»¯Ê¹ÃüÖÎÀí
Linux Systemd Crontab½Ì³Ì£ºÑ§Ï°ÔõÑù×Ô¶¯»¯Ê¹ÃüÖÎÀí£¬ÐèÒªÏêϸ´úÂëʾÀý
СÐò£º
ÔÚLinuxϵͳÖУ¬ÎÒÃǾ³£ÐèÒªÖ´Ðа´ÆÚµÄʹÃü£¬ÀýÈ籸·ÝÊý¾Ý¡¢ÕûÀíÔÝʱÎļþ¡¢×¼Ê±ÖØÆôЧÀ͵ȡ£ÎªÁË×èÖ¹ÊÖ¶¯²Ù×÷£¬ÎÒÃÇ¿ÉÒÔʹÓÃLinuxϵͳÖеÄSystemdºÍCrontab¹¤¾ßÀ´ÊµÏÖʹÃüµÄ×Ô¶¯»¯ÖÎÀí¡£
±¾½Ì³Ì½«ÏÈÈÝÔõÑùʹÓÃSystemdºÍCrontabÀ´×Ô¶¯»¯Ê¹ÃüÖÎÀí£¬²¢ÌṩÏêϸµÄ´úÂëʾÀý¡£
Ò»¡¢Systemd
SystemdÊÇLinuxϵͳÖеÄÒ»¸ö³õʼ»¯ÏµÍ³ºÍЧÀÍÖÎÀíÆ÷£¬Ëü¿ÉÒÔÌṩ×Ô¶¯»¯Ê¹ÃüÖÎÀíµÄ¹¦Ð§¡£ÏÂÃæÊÇÔõÑùʹÓÃSystemdÀ´×Ô¶¯Ö´ÐÐʹÃüµÄ°ì·¨£º
½¨ÉèÒ»¸ö.serviceÎļþ£º
ÔÚ/etc/systemd/system/Ŀ¼Ï½¨ÉèÒ»¸öÒÔ.serviceΪºó׺µÄÎļþ£¬ÀýÈçmytask.service¡£
[Unit] Description=My Task [Service] ExecStart=/path/to/your/script.sh [Install] WantedBy=multi-user.target
µÇ¼ºó¸´ÖÆ
±àд¾ç±¾£º
ÔÚÉÏÃæµÄÉèÖÃÎļþÖУ¬ÎÒÃÇÖ¸¶¨ÁËÒ»¸ö¾ç±¾Îļþscript.shÀ´Ö´ÐÐʹÃü¡£Ôھ籾ÎļþÖУ¬Äã¿ÉÒÔ±àдÐèÒªÖ´ÐеÄʹÃüÂß¼£¬ÀýÈ籸·ÝÊý¾Ý¿â¡¢ÕûÀíÔÝʱÎļþµÈ¡£
ÆôÓúÍÔËÐÐЧÀÍ£º
ÔËÐÐÒÔÏÂÏÂÁîÀ´ÆôÓúÍÔËÐÐЧÀÍ£º
sudo systemctl enable mytask.service sudo systemctl start mytask.service
µÇ¼ºó¸´ÖÆ
ÕâÑù£¬Ð§À;ͻáÔÚϵͳÆô¶¯Ê±×Ô¶¯ÔËÐУ¬²¢ÇÒ»áÒ»Ö±ÔËÐÐÏÂÈ¥£¬Ö±µ½ÄãÊÖ¶¯×èÖ¹¡£
¶þ¡¢Crontab
CrontabÊÇLinuxϵͳÖеÄÒ»¸ö׼ʱʹÃüÖÎÀí¹¤¾ß£¬Ëü¿ÉÒÔƾָ֤¶¨µÄʱ¼ä¾àÀëÀ´Ö´ÐÐʹÃü¡£ÏÂÃæÊÇÔõÑùʹÓÃCrontabÀ´Ö´ÐÐ׼ʱʹÃüµÄ°ì·¨£º
±à¼CrontabÉèÖÃÎļþ£º
ÔËÐÐÒÔÏÂÏÂÁîÀ´±à¼CrontabÉèÖÃÎļþ£º
crontab -e
µÇ¼ºó¸´ÖÆ
±àдʹÃü£º
ÔÚ·¿ªµÄÉèÖÃÎļþÖУ¬Ã¿Ò»Ðдú±íÒ»¸öʹÃü¡£Ã¿Ò»ÐеÄÃûÌÃÈçÏ£º
* * * * * command-to-be-executed
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÃûÌÃÖУ¬Îå¸öÐǺŴú±íÁËʹÃüÖ´ÐеÄʱ¼ä¾àÀ룬»®·ÖÌåÏÖ·ÖÖÓ¡¢Ð¡Ê±¡¢ÈÕÆÚ¡¢Ô·ݺÍÐÇÆÚ¼¸¡£command-to-be-executedÊÇÐèÒªÖ´ÐеÄÏÂÁî»ò¾ç±¾¡£
ÀýÈ磬ÒÔϵÄʾÀýÊÇÌìÌìµÄÆÆÏþÁ½µãÖ´Ðо籾script.sh£º
0 2 * * * /path/to/your/script.sh
µÇ¼ºó¸´ÖÆ
ÉúÑÄÉèÖÃÎļþ£º
±à¼Íê³Éºó£¬ÉúÑÄÉèÖÃÎļþ²¢Í˳ö¡£
Éó²éºÍÖÎÀíʹÃü£º
ÔËÐÐÒÔÏÂÏÂÁîÀ´Éó²éÄ¿½ñËùÓеÄ׼ʱʹÃü£º
crontab -l
µÇ¼ºó¸´ÖÆ
ÈôÊÇÄãÐèҪɾ³ýÒ»¸ö׼ʱʹÃü£¬¿ÉÒÔÔËÐÐÒÔÏÂÏÂÁ
crontab -r
µÇ¼ºó¸´ÖÆ
¿¢ÊÂÓ
±¾½Ì³ÌÏÈÈÝÁËÔõÑùʹÓÃLinuxϵͳÖеÄSystemdºÍCrontab¹¤¾ßÀ´ÊµÏÖ×Ô¶¯»¯Ê¹ÃüÖÎÀí¡£ÎÞÂÛÊÇSystemdÕÕ¾ÉCrontab£¬¶¼ÌṩÁ˼òÆÓ¶øÇ¿Ê¢µÄ¹¦Ð§£¬¿ÉÒÔƾ֤ʹÃüµÄÐèÇóÎÞаµ÷½âÖ´ÐÐʱ¼ä¾àÀë¡£ÈôÊÇÄãÐèÒª°´ÆÚÖ´ÐÐʹÃü£¬¿ÉÒÔƾ֤×Ô¼ºµÄÐèҪѡÔñºÏÊʵŤ¾ß£¬²¢Æ¾Ö¤Ê¾Àý´úÂëÐ޸ĺÍÀ©Õ¹¡£
Ï£Íû±¾½Ì³Ì¶ÔÄãÓÐËù×ÊÖú£¬×£ÄãÔÚLinuxϵͳÖÐÄܹ»ÇáËɵØʵÏÖʹÃüµÄ×Ô¶¯»¯ÖÎÀí£¡
ÒÔÉϾÍÊÇLinux Systemd Crontab½Ì³Ì£ºÑ§Ï°ÔõÑù×Ô¶¯»¯Ê¹ÃüÖÎÀíµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡