如何编写一个每天午夜运行脚本的 cron?

发布于 2024-09-28 19:58:28 字数 52 浏览 4 评论 0原文

我听说 crontab 是一个不错的选择,但是我该如何编写该行以及将其放在服务器上的哪里?

I have heard crontab is a good choice, but how do I write the line and where do I put it on the server?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

幻梦 2024-10-05 19:58:28

这里有一个很好的教程,介绍什么是 crontab 以及如何在 Ubuntu 上使用它。您的 crontab 行将如下所示:(

00 00 * * * ruby path/to/your/script.rb

00 00 表示午夜 - 0 分钟和 0 小时 - * 表示每个月的每一天。)

Syntax: 
  mm hh dd mt wd  command

  mm minute 0-59
  hh hour 0-23
  dd day of month 1-31
  mt month 1-12
  wd day of week 0-7 (Sunday = 0 or 7)
  command: what you want to run
  all numeric values can be replaced by * which means all

Here's a good tutorial on what crontab is and how to use it on Ubuntu. Your crontab line will look something like this:

00 00 * * * ruby path/to/your/script.rb

(00 00 indicates midnight--0 minutes and 0 hours--and the *s mean every day of every month.)

Syntax: 
  mm hh dd mt wd  command

  mm minute 0-59
  hh hour 0-23
  dd day of month 1-31
  mt month 1-12
  wd day of week 0-7 (Sunday = 0 or 7)
  command: what you want to run
  all numeric values can be replaced by * which means all
ぃ弥猫深巷。 2024-10-05 19:58:28

从手册页

linux$ man -S 5 crontab

   cron(8) examines cron entries once every minute.

   The time and date fields are:

          field          allowed values
          -----          --------------
          minute         0-59
          hour           0-23
          day of month   1-31
          month          1-12 (or names, see below)
          day of week    0-7 (0 or 7 is Sun, or use names)
   ...
   # run five minutes after midnight, every day
   5 0 * * *       $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
   ...

最好注意可以使用的特殊“昵称”(在手册页中记录),特别是“@reboot”,它没有时间和日期替代项。

   # Run once after reboot.
   @reboot         /usr/local/sbin/run_only_once_after_reboot.sh

您还可以使用此技巧每分钟多次运行 cron 作业。

   # Run every minute at 0, 20, and 40 second intervals
   * * * * *       sleep 00; /usr/local/sbin/run_3times_per_minute.sh
   * * * * *       sleep 20; /usr/local/sbin/run_3times_per_minute.sh
   * * * * *       sleep 40; /usr/local/sbin/run_3times_per_minute.sh

要添加 cron 作业,您可以执行以下三件事之一:

  1. 将命令添加到用户的 crontab,如上所示(以及来自 crontab,第 5 节,手册页)。

    • 以 root 身份使用 crontab -e -u 编辑用户的 crontab
    • 或仅使用 crontab -e 编辑当前用户的 crontab
    • 您可以使用 EDITOR 环境变量设置编辑器
      • env EDITOR=nano crontab -e -u <用户名>
      • 或为整个 shell 会话设置 EDITOR 的值
        1. 导出 EDITOR=vim
        2. crontab -e
    • 使用 chmod a+x使脚本可执行
  1. 创建一个脚本/程序作为 cron 作业,并将其添加到系统的 anacron /etc/cron.*ly 目录

    • anacron /etc/cron.*ly 目录:
      • /etc/cron.daily
      • /etc/cron.hourly
      • /etc/cron.monthly
      • /etc/cron.weekly
    • 如:
      • /etc/cron.daily/script_runs_daily.sh
      • chmod a+x /etc/cron.daily/script_runs_daily.sh -- 使其可执行
    • 另请参阅 anacron 手册页:man anacron
    • 使用 chmod a+x使脚本可执行
    • 这些 cron.*ly 脚本何时运行?
      • 对于 RHEL/CentOS 5.x,它们在 /etc/crontab/etc/anacrontab 中配置为在设定时间运行
      • RHEL/CentOS 6.x+ 和 Fedora 17+ Linux 系统仅在 /etc/anacrontab 中定义此项,并在 /etc/cron.d/0hourly 中定义 cron.hourly代码>

  1. 或者,可以在/etc/cron.d中创建系统crontables。

    • 将前面描述的 crontab 语法(另外提供用户来执行每个作业)放入一个文件中,并将该文件放入 /etc/cron.d 目录中。
    • 这些在系统包(例如 RPM 包)中很容易管理,因此通常可能是特定于应用程序的。
    • 语法差异在于,必须在时间/日期字段之后、要执行的命令之前为 cron 作业指定用户。
    • 添加到 /etc/cron.d 的文件不需要是可执行的。
    • 下面是一个以用户 someuser 身份执行的示例作业,并强制使用 /bin/bash 作为 shell。

   File: /etc/cron.d/myapp-cron
   # use /bin/bash to run commands, no matter what /etc/passwd says
   SHELL=/bin/bash
   # Execute a nightly (11:00pm) cron job to scrub application records
   00 23 * * * someuser /opt/myapp/bin/scrubrecords.php

from the man page

linux$ man -S 5 crontab

   cron(8) examines cron entries once every minute.

   The time and date fields are:

          field          allowed values
          -----          --------------
          minute         0-59
          hour           0-23
          day of month   1-31
          month          1-12 (or names, see below)
          day of week    0-7 (0 or 7 is Sun, or use names)
   ...
   # run five minutes after midnight, every day
   5 0 * * *       $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
   ...

It is good to note the special "nicknames" that can be used (documented in the man page), particularly "@reboot" which has no time and date alternative.

   # Run once after reboot.
   @reboot         /usr/local/sbin/run_only_once_after_reboot.sh

You can also use this trick to run your cron job multiple times per minute.

   # Run every minute at 0, 20, and 40 second intervals
   * * * * *       sleep 00; /usr/local/sbin/run_3times_per_minute.sh
   * * * * *       sleep 20; /usr/local/sbin/run_3times_per_minute.sh
   * * * * *       sleep 40; /usr/local/sbin/run_3times_per_minute.sh

To add a cron job, you can do one of three things:

  1. add a command to a user's crontab, as shown above (and from the crontab, section 5, man page).

    • edit a user's crontab as root with crontab -e -u <username>
    • or edit the current user's crontab with just crontab -e
    • You can set the editor with the EDITOR environment variable
      • env EDITOR=nano crontab -e -u <username>
      • or set the value of EDITOR for your entire shell session
        1. export EDITOR=vim
        2. crontab -e
    • Make scripts executable with chmod a+x <file>

  1. create a script/program as a cron job, and add it to the system's anacron /etc/cron.*ly directories

    • anacron /etc/cron.*ly directories:
      • /etc/cron.daily
      • /etc/cron.hourly
      • /etc/cron.monthly
      • /etc/cron.weekly
    • as in:
      • /etc/cron.daily/script_runs_daily.sh
      • chmod a+x /etc/cron.daily/script_runs_daily.sh -- make it executable
    • See also the anacron man page: man anacron
    • Make scripts executable with chmod a+x <file>
    • When do these cron.*ly script run?
      • For RHEL/CentOS 5.x, they are configured in /etc/crontab or /etc/anacrontab to run at a set time
      • RHEL/CentOS 6.x+ and Fedora 17+ Linux systems only define this in /etc/anacrontab, and define cron.hourly in /etc/cron.d/0hourly

  1. Or, One can create system crontables in /etc/cron.d.

    • The previously described crontab syntax (with additionally providing a user to execute each job as) is put into a file, and the file is dropped into the /etc/cron.d directory.
    • These are easy to manage in system packaging (e.g. RPM packages), so may usually be application specific.
    • The syntax difference is that a user must be specified for the cron job after the time/date fields and before the command to execute.
    • The files added to /etc/cron.d do not need to be executable.
    • Here is an example job that is executed as the user someuser, and the use of /bin/bash as the shell is forced.

   File: /etc/cron.d/myapp-cron
   # use /bin/bash to run commands, no matter what /etc/passwd says
   SHELL=/bin/bash
   # Execute a nightly (11:00pm) cron job to scrub application records
   00 23 * * * someuser /opt/myapp/bin/scrubrecords.php
蓝梦月影 2024-10-05 19:58:28

设置 cron 作业的快速指南

创建一个新的文本文件,例如:mycronjobs.txt

对于每个日常作业(00:00、03:45),保存计划行mycronjobs.txt

00 00 * * * ruby path/to/your/script.rb
45 03 * * * path/to/your/script2.sh

将作业发送到 cron(每次运行此命令时,cron 都会删除已存储的内容并使用 mycronjobs.txt 中的新信息进行更新)

crontab mycronjobs.txt

额外有用信息

查看当前的 cron 作业

crontab -l

删除所有 cron 作业

crontab -r

Quick guide to setup a cron job

Create a new text file, example: mycronjobs.txt

For each daily job (00:00, 03:45), save the schedule lines in mycronjobs.txt

00 00 * * * ruby path/to/your/script.rb
45 03 * * * path/to/your/script2.sh

Send the jobs to cron (everytime you run this, cron deletes what has been stored and updates with the new information in mycronjobs.txt)

crontab mycronjobs.txt

Extra Useful Information

See current cron jobs

crontab -l

Remove all cron jobs

crontab -r
记忆で 2024-10-05 19:58:28

有时您需要使用 crontab 和 rvm 来指定 PATH 和 GEM_PATH。

像这样:

# top of crontab file
PATH=/home/user_name/.rvm/gems/ruby-2.2.0/bin:/home/user_name/.rvm/gems/ruby-2.2.0@global/bin:/home/user_name/.rvm/rubies/ruby-2.2.$
GEM_PATH=/home/user_name/.rvm/gems/ruby-2.2.0:/home/user_name/.rvm/gems/ruby-2.2.0@global

# jobs
00 00 * * * ruby path/to/your/script.rb
00 */4 * * * ruby path/to/your/script2.rb
00 8,12,22 * * * ruby path/to/your/script3.rb

Sometimes you'll need to specify PATH and GEM_PATH using crontab with rvm.

Like this:

# top of crontab file
PATH=/home/user_name/.rvm/gems/ruby-2.2.0/bin:/home/user_name/.rvm/gems/ruby-2.2.0@global/bin:/home/user_name/.rvm/rubies/ruby-2.2.$
GEM_PATH=/home/user_name/.rvm/gems/ruby-2.2.0:/home/user_name/.rvm/gems/ruby-2.2.0@global

# jobs
00 00 * * * ruby path/to/your/script.rb
00 */4 * * * ruby path/to/your/script2.rb
00 8,12,22 * * * ruby path/to/your/script3.rb
淡淡绿茶香 2024-10-05 19:58:28

您可以通过两种方式执行shell脚本,使用cron作业或编写shell脚本

让我们假设您的脚本名称是“yourscript.sh”

首先检查脚本的用户权限。
使用下面的命令检查脚本的用户权限

ll script.sh

如果脚本位于 root 中,则使用下面的命令

sudo crontab -e

其次,如果脚本拥有用户“ubuntu”,然后使用下面的命令

crontab -e

在 crontab 中添加以下行:-

55 23 * * * /path/to/yourscript.sh

另一种方法这样做是编写一个脚本并在后台运行它

这是您必须在其中放置脚本名称的脚本(例如:- youscript.sh),该脚本将在每天下午 23:55 运行


#!/bin/bash
虽然是真的

/home/modassir/yourscript.sh
睡眠1天
完毕

将其保存在一个文件中(让其命名为“every-day.sh”)

sleep 1d - 意味着它等待一天,然后再次运行。

现在授予您的脚本权限。使用以下命令:-

chmod +x every-day.sh

现在,使用“nohup”在后台执行此 shell 脚本。
即使您从会话注销后,这也会继续执行脚本。

使用下面的命令来执行脚本。

nohup ./every-day.sh &

注意:- 要在每天下午 23:55 运行“yourscript.sh”,您必须在下午 23:55 准时执行“every-day.sh”脚本。

You can execute shell script in two ways,either by using cron job or by writing a shell script

Lets assume your script name is "yourscript.sh"

First check the user permission of the script.
use below command to check user permission of the script

ll script.sh

If the script is in root,then use below command

sudo crontab -e

Second if the script holds the user "ubuntu", then use below command

crontab -e

Add the following line in your crontab:-

55 23 * * * /path/to/yourscript.sh

Another way of doing this is to write a script and run it in the backgroud

Here is the script where you have to put your script name(eg:- youscript.sh) which is going to run at 23:55pm everyday


#!/bin/bash
while true
do
/home/modassir/yourscript.sh
sleep 1d
done

save it in a file (lets name it "every-day.sh")

sleep 1d - means it waits for one day and then it runs again.

now give the permission to your script.use below command:-

chmod +x every-day.sh

now, execute this shell script in the background by using "nohup".
This will keep executing the script even after you logout from your session.

use below command to execute the script.

nohup ./every-day.sh &

Note:- to run "yourscript.sh" at 23:55pm everyday,you have to execute "every-day.sh" script at exactly 23:55pm.

瞎闹 2024-10-05 19:58:28

将这句话放入 crontab 文件中:
0 0 * * * /usr/local/bin/python /opt/ByAccount.py > /var/log/cron.log 2>&1

Put this sentence in a crontab file:
0 0 * * * /usr/local/bin/python /opt/ByAccount.py > /var/log/cron.log 2>&1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文