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

ÔõÑùÔÚLinuxϵͳÖÐʹÓÃSystemdÖÎÀíCrontabʹÃü

ÔõÑùÔÚLinuxϵͳÖÐʹÓÃSystemdÖÎÀíCrontabʹÃü

СÐò£º

ÔÚLinuxϵͳÖÐ £¬CrontabÊÇÒ»¸ö³£ÓõÄ׼ʱʹÃüµ÷Àí¹¤¾ß¡£ËüÔÊÐíÓû§ÔÚÖ¸¶¨µÄʱ¼äÖ´ÐÐÌض¨µÄÏÂÁî»ò¾ç±¾¡£È»¶ø £¬Ëæ×Åʱ¼äµÄÍÆÒÆ £¬ÖÎÀí´ó×ÚCrontabʹÃü±äµÃÔ½À´Ô½ÖØ´ó £¬ÎÒÃÇÐèÒªÒ»ÖÖ¸üºÃµÄ·½·¨À´ÖÎÀíÕâЩʹÃü¡£SystemdÊÇÒ»¸öÔÚÏÖ´úLinuxϵͳÖÐÖÎÀíºÍÆô¶¯ÏµÍ³Ð§À͵Ť¾ß £¬ËüÌṩÁËÒ»ÖÖ¸üÎÞаºÍ¿É¿¿µÄ·½·¨À´ÖÎÀí׼ʱʹÃü¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃSystemdÀ´ÖÎÀíCrontabʹÃü £¬²¢ÌṩÏêϸµÄ´úÂëʾÀý¡£

µÚÒ»²½£º½¨ÉèCrontabʹÃüÎļþ

Ê×ÏÈ £¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öCrontabʹÃüÎļþ¡£Õâ¸öÎļþ½«°üÀ¨ÎÒÃÇÒª°´ÆÚÖ´ÐеÄÏÂÁî»ò¾ç±¾¡£ÀýÈç £¬ÎÒÃÇ¿ÉÒÔ½¨ÉèÒ»¸öÃûΪmytask.shµÄ¾ç±¾Îļþ £¬²¢Ìí¼ÓһЩҪִÐеÄÏÂÁî £¬ÈçÏÂËùʾ£º

#!/bin/bash
echo "Hello, World!" >> /tmp/output.txt

µÇ¼ºó¸´ÖÆ

ÉúÑIJ¢¹Ø±ÕÎļþ¡£È·±£¸ÃÎļþ¾ßÓÐÖ´ÐÐȨÏÞ £¬¿ÉÒÔʹÓÃÏÂÁîchmod +x mytask.shÀ´Ìí¼ÓÖ´ÐÐȨÏÞ¡£

µÚ¶þ²½£º½¨ÉèSystemdЧÀÍÎļþ

½ÓÏÂÀ´ £¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öSystemdЧÀÍÎļþ £¬ÒÔ±ãSystemd¿ÉÒÔÖÎÀíÎÒÃǵÄCrontabʹÃü¡£ÔÚ/etc/systemd/system/Ŀ¼Ï½¨ÉèÒ»¸öÃûΪmytask.serviceµÄÎļþ £¬²¢ÔÚÆäÖÐÌí¼ÓÒÔÏÂÄÚÈÝ£º

[Unit]
Description=MyTask

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

[Install]
WantedBy=multi-user.target

µÇ¼ºó¸´ÖÆ

È·±£½«/path/to/mytask.shÌ滻ΪÏÖʵ¾ç±¾ÎļþµÄ·¾¶¡£

µÚÈý²½£ºÆô¶¯ºÍ²âÊÔʹÃü

ÉúÑIJ¢¹Ø±Õmytask.serviceÎļþºó £¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÆô¶¯Ê¹Ãü£º

sudo systemctl start mytask.service

µÇ¼ºó¸´ÖÆ

ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî¼ì²éʹÃüµÄ״̬£º

sudo systemctl status mytask.service

µÇ¼ºó¸´ÖÆ

ÈôÊÇÒ»ÇÐÕý³£ £¬ÄãÓ¦¸Ã¿´µ½Ê¹ÃüµÄ״̬Ϊ”active (running)”¡£

µÚËIJ½£ºÉèÖÃʹÃü×ÔÆô¶¯

ÈôÊÇÄãÏ£ÍûÔÚϵͳÆô¶¯Ê±×Ô¶¯Æô¶¯Ê¹Ãü £¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁʹÃüÉèÖÃΪ×ÔÆô¶¯£º

sudo systemctl enable mytask.service

µÇ¼ºó¸´ÖÆ

ÏÖÔÚ £¬Ê¹Ãü½«ÔÚϵͳÆô¶¯Ê±×Ô¶¯Æô¶¯¡£

µÚÎå²½£ºÖÎÀíʹÃü

ʹÓÃSystemdÖÎÀíCrontabʹÃüÓÐһЩÌØÁíÍâÓÅÊÆ¡£ÎÒÃÇ¿ÉÒÔʹÓÃsystemctlÏÂÁîÀ´Æô¶¯¡¢×èÖ¹¡¢ÖØÆôºÍÖØÔØʹÃü £¬¶øÎÞÐèÊÖ¶¯±à¼­CrontabÎļþ¡£ÒÔÏÂÊÇһЩ³£ÓõÄÏÂÁîʾÀý£º

# Æô¶¯Ê¹Ãü
sudo systemctl start mytask.service

# ×èֹʹÃü
sudo systemctl stop mytask.service

# ÖØÆôʹÃü
sudo systemctl restart mytask.service

# ÖØÔØʹÃü£¨ÖØмÓÔØÉèÖÃÎļþ£©
sudo systemctl reload mytask.service

µÇ¼ºó¸´ÖÆ

ͨ¹ýʹÓÃSystemd £¬ÎÒÃÇ¿ÉÒÔ¸ü¾«Á·ºÍÇåÎúµØÖÎÀíCrontabʹÃü £¬²¢ïÔÌ­ÁËÊÖ¶¯±à¼­CrontabÎļþµÄΣº¦¡£

½áÂÛ£º

ÔÚ±¾ÎÄÖÐ £¬ÎÒÃÇÏÈÈÝÁËÔõÑùʹÓÃSystemdÀ´ÖÎÀíCrontabʹÃü¡£Í¨¹ý½¨ÉèÒ»¸öSystemdЧÀÍÎļþ²¢Ê¹ÓÃsystemctlÏÂÁîÀ´Æô¶¯¡¢×èÖ¹¡¢ÖØÆôºÍÖØÔØʹÃü £¬ÎÒÃÇ¿ÉÒÔ¸üÀû±ãµØÖÎÀí׼ʱʹÃü¡£ÕâÖÖ·½·¨ÌṩÁ˸üÎÞаºÍ¿É¿¿µÄÖÎÀí·½·¨ £¬ïÔÌ­ÁËÊÖ¶¯±à¼­CrontabÎļþµÄΣº¦¡£Ï£Íû±¾ÎĶÔÄãÔÚLinuxϵͳÖÐÖÎÀíCrontabʹÃüʱÓÐËù×ÊÖú¡£

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

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

QR code
ÍøÕ¾µØͼ