{"id":12458,"date":"2018-09-10T10:00:05","date_gmt":"2018-09-10T13:00:05","guid":{"rendered":"https:\/\/blog.hostinger.io\/pt-tutoriais\/?p=12458"},"modified":"2023-07-21T16:01:23","modified_gmt":"2023-07-21T19:01:23","slug":"como-configurar-cron-jobs-no-linux","status":"publish","type":"post","link":"\/pt\/tutoriais\/como-configurar-cron-jobs-no-linux\/","title":{"rendered":"Como configurar cron jobs no Linux (VPS)"},"content":{"rendered":"<p><span style=\"font-weight: 400\">Como configurar Cron Jobs no Linux (VPS) pode fazer com que economize muito tempo.&nbsp;<\/span><\/p><p><span style=\"font-weight: 400\"> Muitas pessoas realizam tarefas manuais no Linux, como por exemplo enviar um email com a quantidade de disco utilizada para si mesmo, arquivar logs, deletar diret&oacute;rios e arquivos tempor&aacute;rios, etc. Mas por que perder tempo com tarefas que podem ser feitas automaticamente? &Eacute; para isso que existe o <\/span><b>Cron<\/b><span style=\"font-weight: 400\">, um agendador e executor de tarefas. Administradores de sistema usam tarefas cron (tamb&eacute;m conhecidas como cron jobs) para agendar tarefas recorrentes, como comandos ou scripts de shell, para rodar periodicamente em dias, hor&aacute;rios e per&iacute;odos previamente estabelecidos. Este tutorial vai ensinar como configurar <\/span><b>Cron Job<\/b><span style=\"font-weight: 400\"> no Linux.<\/span><\/p><h3><b>O que voc&ecirc; vai precisar<\/b><\/h3><p><span style=\"font-weight: 400\">Para aprender como configurar Cron Jobs no Linux voc&ecirc; vai precisar de:<\/span><\/p><ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Acesso SSH ao seu servi&ccedil;o de <a href=\"\/pt\/alojamento-vps\">VPS anual<\/a><\/span><\/li>\n<\/ul><h2 id=\"h-1-passo-entender-a-sintaxe\"><b>1&deg; Passo &mdash; Entender a Sintaxe<\/b><\/h2><p><span style=\"font-weight: 400\">O servi&ccedil;o <\/span><b>Cron<\/b><span style=\"font-weight: 400\"> usa o arquivo <\/span><b>Crontab<\/b><span style=\"font-weight: 400\"> (tabela Cron) que geralmente &eacute; localizado no diret&oacute;rio <\/span><span style=\"font-weight: 400\">\/etc\/crontab<\/span><span style=\"font-weight: 400\">, <\/span><span style=\"font-weight: 400\">\/var\/cron\/tabs<\/span><span style=\"font-weight: 400\"> ou <\/span><span style=\"font-weight: 400\">\/var\/spool\/cron\/<\/span><span style=\"font-weight: 400\">. Cada linha no Crontab representa uma tarefa agendada. &Eacute; s&oacute; rodar este comando no terminal para visualizar todas as tarefas agendadas:<\/span><\/p><pre><span style=\"font-weight: 400\">crontab -l<\/span><\/pre><p><span style=\"font-weight: 400\">Se o arquivo estiver vazio, n&atilde;o se preocupe, significa apenas que voc&ecirc; n&atilde;o tem nenhuma tarefa agendada. Um exemplo de cron job seria:<\/span><\/p><pre><span style=\"font-weight: 400\">0 * * * * \/usr\/local\/cpanel\/scripts\/cpbackup<\/span><\/pre><p><span style=\"font-weight: 400\">Neste exemplo, um script de backup do cPanel &eacute; executado de hora em hora. Como voc&ecirc; pode ver, a linha consiste de 6 campos:<\/span><\/p><pre><span style=\"font-weight: 400\">&#9484;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472; min (0 - 59)<\/span>\n<span style=\"font-weight: 400\">&#9474; &#9484;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472; hora (0 - 23)<\/span>\n<span style=\"font-weight: 400\">&#9474; &#9474; &#9484;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472; dia do m&ecirc;s (1 - 31)<\/span>\n<span style=\"font-weight: 400\">&#9474; &#9474; &#9474; &#9484;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472; m&ecirc;s (1 - 12)<\/span>\n<span style=\"font-weight: 400\">&#9474; &#9474; &#9474; &#9474; &#9484;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472; dia da semana (0 - 6) (0 a 6 representa<\/span>\n<span style=\"font-weight: 400\">&#9474; &#9474; &#9474; &#9474; &#9474; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;de domingo a s&aacute;bado, ou use nomes;<\/span>\n<span style=\"font-weight: 400\">&#9474; &#9474; &#9474; &#9474; &#9474; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7 tamb&eacute;m representa domingo)<\/span>\n<span style=\"font-weight: 400\">&#9474; &#9474; &#9474; &#9474; &#9474;<\/span>\n<span style=\"font-weight: 400\">0 1 * * * \/usr\/local\/cpanel\/scripts\/cpbackup<\/span><\/pre><p><span style=\"font-weight: 400\">Se ainda tiver d&uacute;vidas confira as p&aacute;ginas do manual do <\/span><b>Cron<\/b><span style=\"font-weight: 400\"> executando o comando <\/span><span style=\"font-weight: 400\">man cron<\/span><span style=\"font-weight: 400\"> em seu console.<\/span><\/p><h2 id=\"h-2-passo-criar-a-cron-job\"><b>2&deg; Passo &mdash; Criar a Cron job<\/b><\/h2><p><span style=\"font-weight: 400\">Edite o crontab com o comando:<\/span><\/p><pre><span style=\"font-weight: 400\">crontab -e<\/span><\/pre><p><div class=\"announcement-block announcement-block--important\">\n            <span class=\"announcement-block__heading\">\n                <svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                    <path fill-rule=\"evenodd\" clip-rule=\"evenodd\"\n                          d=\"M12 22.5C17.799 22.5 22.5 17.799 22.5 12C22.5 6.20101 17.799 1.5 12 1.5C6.20101 1.5 1.5 6.20101 1.5 12C1.5 17.799 6.20101 22.5 12 22.5ZM13.637 7.65198C13.637 6.74791 12.9041 6.01501 12 6.01501C11.0959 6.01501 10.363 6.74791 10.363 7.65198C10.5335 9.53749 10.875 13.383 10.875 13.383C10.875 14.0043 11.3787 14.508 12 14.508C12.6213 14.508 13.125 14.0043 13.125 13.383V13.38L13.637 7.65198ZM11.9927 15.714C11.3714 15.714 10.8677 16.2177 10.8677 16.839C10.8677 17.4603 11.3714 17.964 11.9927 17.964H12.0073C12.6286 17.964 13.1323 17.4603 13.1323 16.839C13.1323 16.2177 12.6286 15.714 12.0073 15.714H11.9927Z\"\n                          fill=\"#FEA419\"\/>\n                <\/svg>\n                Important\n            <\/span>\n            <p class=\"announcement-block__content\">\n                <span style=\"font-weight: 400\">IMPORTANTE! N&atilde;o edite o arquivo crontab diretamente, sempre execute o comando <\/span><span style=\"font-weight: 400\">crontab -e<\/span><span style=\"font-weight: 400\">.<\/span>\n            <\/p><\/div><br>\n<span style=\"font-weight: 400\">Agora fa&ccedil;a a lista de scripts que devem ser executados em algum momento espec&iacute;fico. Use a estrutura fornecida no <\/span><b>Passo 1<\/b><span style=\"font-weight: 400\">. Aqui temos alguns exemplos:<\/span><\/p><ol>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Executar em per&iacute;odos recorrentes:<\/span>\n<ul>\n<li style=\"font-weight: 400\">\n<pre><span style=\"font-weight: 400\">0 0 1 * *<\/span><span style=\"font-weight: 400\"> &ndash; no primeiro dia de cada m&ecirc;s<\/span><\/pre>\n<\/li>\n<li style=\"font-weight: 400\">\n<pre><span style=\"font-weight: 400\">0 0 * * 0<\/span><span style=\"font-weight: 400\"> &ndash; uma vez por semana<\/span><\/pre>\n<\/li>\n<li style=\"font-weight: 400\">\n<pre><span style=\"font-weight: 400\">0 0 * * *<\/span><span style=\"font-weight: 400\"> &ndash; uma vez por dia &agrave; meia-noite<\/span><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Executar em uma hora\/dia espec&iacute;fica:<\/span>\n<ul>\n<li style=\"font-weight: 400\">\n<pre><span style=\"font-weight: 400\">20 22 3 * *<\/span><span style=\"font-weight: 400\"> &ndash; no terceiro dia de cada m&ecirc;s &agrave;s 22:20<\/span><\/pre>\n<\/li>\n<li style=\"font-weight: 400\">\n<pre><span style=\"font-weight: 400\">*\/30 * * * *<\/span><span style=\"font-weight: 400\"> &ndash; a cada 30 minutos<\/span><\/pre>\n<\/li>\n<li style=\"font-weight: 400\">\n<pre><span style=\"font-weight: 400\">0 8 * * 1,2,3,4,5<\/span><span style=\"font-weight: 400\"> de segunda a sexta &agrave;s 08:00<\/span><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol><p><span style=\"font-weight: 400\">Ap&oacute;s o agendamento n&atilde;o esque&ccedil;a de colocar o caminho para o seu script. Voc&ecirc; pode gerar tabelas no site <\/span><a href=\"http:\/\/crontab-generator.org\/\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400\">http:\/\/crontab-generator.org<\/span><\/a><\/p><p><span style=\"font-weight: 400\">Quando conclu&iacute;do, feche a crontab com o comando:<\/span><\/p><pre><span style=\"font-weight: 400\">:wq<\/span><\/pre><h3><b>Conclus&atilde;o<\/b><\/h3><p><span style=\"font-weight: 400\">Agora voc&ecirc; j&aacute; sabe como configurar Cron Jobs no Linux,<\/span><span style=\"font-weight: 400\">&nbsp;automatizar a execu&ccedil;&atilde;o de seus scripts e comandos no Linux e como especificar a hora, data e recorr&ecirc;ncia.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Como configurar Cron Jobs no Linux (VPS) pode fazer com que economize muito tempo.&nbsp; Muitas pessoas realizam tarefas manuais no Linux, como por exemplo enviar um email com a quantidade de disco utilizada para si mesmo, arquivar logs, deletar diret&oacute;rios e arquivos tempor&aacute;rios, etc. Mas por que perder tempo com tarefas que podem ser feitas [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/pt\/tutoriais\/como-configurar-cron-jobs-no-linux\/\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":78,"featured_media":12459,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"rank_math_title":"","rank_math_description":"Como configurar Cron Jobs no Linux (VPS) e pare de perder tempo com tarefas manuais. O Cron \u00e9 um agendador e executor de tarefas, e vai otimizar o seu tempo.","rank_math_focus_keyword":"como configurar Cron Jobs no Linux","footnotes":""},"categories":[7740,7708],"tags":[5901,5900],"class_list":["post-12458","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-gerenciamento-e-seguranca","category-vps","tag-configurar-cron-jobs-no-linux","tag-criar-cron-job"],"hreflangs":[{"locale":"pt-BR","link":"https:\/\/www.hostinger.com\/br\/tutoriais\/como-configurar-cron-jobs-no-linux","default":1},{"locale":"pt-PT","link":"https:\/\/www.hostinger.com\/pt\/tutoriais\/como-configurar-cron-jobs-no-linux","default":0}],"acf":[],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/pt\/tutoriais\/wp-json\/wp\/v2\/posts\/12458","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/pt\/tutoriais\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/pt\/tutoriais\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/pt\/tutoriais\/wp-json\/wp\/v2\/users\/78"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/pt\/tutoriais\/wp-json\/wp\/v2\/comments?post=12458"}],"version-history":[{"count":4,"href":"https:\/\/www.hostinger.com\/pt\/tutoriais\/wp-json\/wp\/v2\/posts\/12458\/revisions"}],"predecessor-version":[{"id":38452,"href":"https:\/\/www.hostinger.com\/pt\/tutoriais\/wp-json\/wp\/v2\/posts\/12458\/revisions\/38452"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/pt\/tutoriais\/wp-json\/wp\/v2\/media\/12459"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/pt\/tutoriais\/wp-json\/wp\/v2\/media?parent=12458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/pt\/tutoriais\/wp-json\/wp\/v2\/categories?post=12458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/pt\/tutoriais\/wp-json\/wp\/v2\/tags?post=12458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}