×ðÁú¿­Ê±

ÔõÑùÔÚLinuxϵͳÖÐʹÓÃSystemdºÍCrontabʵÏÖϵͳ×ÔÆô¶¯

ÔõÑùÔÚLinuxϵͳÖÐʹÓÃSystemdºÍCrontabʵÏÖϵͳ×ÔÆô¶¯

СÐò£º

ÔÚLinuxϵͳÖÐ £¬ÎÒÃǾ­³£ÐèÒª½«Ò»Ð©³£ÓõÄЧÀÍ»ò¾ç±¾ÉèÖÃΪϵͳ×ÔÆô¶¯ £¬ÒÔ±ãϵͳÖØÆôºóÄܹ»×Ô¶¯ÔËÐС£ÔÚ±¾ÎÄÖÐ £¬½«ÏÈÈÝÔõÑùʹÓÃSystemdºÍCrontabÕâÁ½¸ö¹¤¾ßÀ´ÊµÏÖϵͳ×ÔÆô¶¯ £¬²¢¸ø³öÏêϸµÄ´úÂëʾÀý¡£

Ò»¡¢SystemdµÄʹÓÃ

SystemdÊÇLinux²Ù×÷ϵͳÖг£ÓõÄϵͳºÍЧÀÍÖÎÀí¹¤¾ß¡£ËüÌṩÁËÀû±ãµÄÉèÖýӿÚ £¬¿ÉÒÔÇáËÉÖÎÀíºÍ¿ØÖÆϵͳµÄÖÖÖÖЧÀÍ¡£

±àдһ¸öSystemdЧÀ͵¥Î»Îļþ

Ê×ÏÈ £¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öSystemdЧÀ͵¥Î»Îļþ £¬¸ÃÎļþ½ç˵ÁËÎÒÃÇÒª×ÔÆô¶¯µÄЧÀ͵ÄÏà¹ØÐÅÏ¢¡£Ð½¨Ò»¸öÒÔЧÀÍÃûÃüÃûµÄÎļþ £¬ºó׺Ϊ.service £¬ÀýÈçmy_service.service¡£ÒÔÏÂÊÇÒ»¸öʾÀýµÄServiceµ¥Î»ÎļþµÄÄÚÈÝ£º

[Unit]
Description=My Service
After=network.target

[Service]
ExecStart=/path/to/your_script.sh
Type=simple
Restart=always
User=root

[Install]
WantedBy=default.target

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

ÔÚÉÏÃæµÄʾÀýÖÐ £¬DescriptionÓÃÓÚÐÎòЧÀ͵ÄÃû³Æ £¬AfterÖ¸¶¨ÁËЧÀÍÆô¶¯µÄÒÀÀµ¹Øϵ £¬ExecStartÖ¸¶¨ÁËЧÀÍÆô¶¯Ê±ÒªÖ´Ðеľ籾»ò³ÌÐò £¬TypeÖ¸¶¨ÁËЧÀ͵ÄÀàÐÍ £¬RestartÖ¸¶¨ÁËЧÀÍ×èÖ¹ºóÊÇ·ñ×Ô¶¯ÖØÆô £¬UserÖ¸¶¨ÁËЧÀÍÔËÐеÄÓû§¡£

ÆäÖÐ £¬WantedByÊÇÒ»¸öÁ´½ÓÄ¿µÄ £¬ÓÃÓÚÖ¸¶¨systemdÓ¦¸ÃÔÚÄĸö.targetÏÂ×Ô¶¯Æô¶¯Õâ¸öЧÀÍ £¬Ä¬ÈÏÇéÐÎÏÂΪdefault.target¡£

½«Ð§ÀÍÎļþ·ÅÈëϵͳµÄSystemdĿ¼ÖÐ

½«±àдºÃµÄЧÀ͵¥Î»Îļþ £¬Òƶ¯µ½ÏµÍ³µÄSystemdЧÀÍĿ¼Ï £¬Ò»Ñùƽ³£Îª/etc/systemd/system/¡£ÔÚÖÕ¶ËÖÐÖ´ÐÐÒÔÏÂÏÂÁîÍê³É²Ù×÷£º

sudo mv my_service.service /etc/systemd/system/

µÇ¼ºó¸´ÖÆ

Æô¶¯Ð§ÀͲ¢ÉèÖÃ×ÔÆô¶¯

Íê³ÉÒÔÉÏ°ì·¨ºó £¬¾Í¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´Æô¶¯ºÍ×èֹЧÀÍÁË£º

sudo systemctl start my_service
sudo systemctl stop my_service

µÇ¼ºó¸´ÖÆ

ΪÁËÉèÖøÃЧÀÍΪϵͳ×ÔÆô¶¯ £¬Ö»ÐèÖ´ÐÐÒÔÏÂÏÂÁ

sudo systemctl enable my_service

µÇ¼ºó¸´ÖÆ

ÕâÑù £¬µ±ÏµÍ³ÖØÆôºó £¬¸ÃЧÀͽ«×Ô¶¯Æô¶¯¡£

¶þ¡¢CrontabµÄʹÓÃ

CrontabÊÇÒ»¸öÓÃÀ´ÔÚLinuxϵͳÖа´ÆÚÖ´ÐÐʹÃüµÄ¹¤¾ß¡£ÎÒÃÇ¿ÉÒÔʹÓÃCrontabÀ´ÉèÖÃϵͳÖØÆôºóÒª×Ô¶¯Ö´ÐеÄʹÃü¡£

±à¼­CrontabÎļþ

ʹÓÃÒÔÏÂÏÂÁî±à¼­Ä¿½ñÓû§µÄCrontabÎļþ£º

crontab -e

µÇ¼ºó¸´ÖÆ

Ôڱ༭Æ÷ÖÐÌí¼ÓÒÔÏÂÄÚÈÝ£º

@reboot /path/to/your_script.sh

µÇ¼ºó¸´ÖÆ

ÆäÖÐ@rebootÌåÏÖÔÚϵͳÆô¶¯ºóÖ´ÐÐ £¬/path/to/your_script.shÊÇÒªÖ´Ðеľ籾»ò³ÌÐòµÄ·¾¶¡£

ÉúÑIJ¢Í˳ö±à¼­Æ÷

ÉúÑIJ¢Í˳ö±à¼­Æ÷ºó £¬Crontab»á×Ô¶¯ÉúЧ¡£

ÑéÖ¤ÉèÖÃ

ʹÓÃÒÔÏÂÏÂÁî¿ÉÒÔÉó²éÄ¿½ñÓû§µÄCrontabÉèÖãº

crontab -l

µÇ¼ºó¸´ÖÆ

ÈôÊÇÄܹ»¿´µ½ÊʲÅÌí¼ÓµÄÄÚÈÝ £¬ÔòÌåÏÖÉèÖÃÀֳɡ£

½áÂÛ£º

ͨ¹ýʹÓÃSystemdºÍCrontab¹¤¾ß £¬ÎÒÃÇ¿ÉÒÔÀû±ãµØʵÏÖ Linux ϵͳÖØÆôºóµÄ×Ô¶¯Æô¶¯¹¦Ð§¡£Ê¹Óà Systemd ¿ÉÒÔÖÎÀíϵͳµÄÖÖÖÖЧÀÍ £¬²¢ÇÒ¿ÉÒÔͨ¹ý¼òÆÓµÄÉèÖÃÎļþÀ´ÊµÏÖ×Ô¶¯Æô¶¯¡£¶øʹÓà Crontab Ôò¿ÉÒÔʵÏÖ°´ÆÚÖ´ÐÐʹÃüµÄ¹¦Ð§¡£

´úÂëʾÀý£º

ÒÔÏÂÊÇÒ»¸öʾÀýµÄ¾ç±¾ £¬Õ¹Ê¾ÁËÔõÑùʹÓÃSystemdºÍCrontabÀ´ÊµÏÖϵͳ×ÔÆô¶¯µÄ¹¦Ð§¡£

SystemdЧÀ͵¥Î»Îļþ my_service.service£º

[Unit]
Description=My Service
After=network.target

[Service]
ExecStart=/path/to/your_script.sh
Type=simple
Restart=always
User=root

[Install]
WantedBy=default.target

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

CrontabÉèÖÃÎļþ£º

@reboot /path/to/your_script.sh

µÇ¼ºó¸´ÖÆ

ÒÔÉϾÍÊǹØÓÚÔõÑùÔÚLinuxϵͳÖÐʹÓÃSystemdºÍCrontabÀ´ÊµÏÖϵͳ×ÔÆô¶¯µÄÒªÁìºÍÏêϸ´úÂëʾÀý¡£Ï£Íû±¾ÎÄÄܹ»¶ÔÄãÓÐËù×ÊÖú¡£

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

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

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

13452372176

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

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

QR code
¡¾ÍøÕ¾µØͼ¡¿¡¾sitemap¡¿