{"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><p class=\"important\"><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><\/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":[4915],"tags":[5901,5900],"class_list":["post-12458","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","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}]}}