ubuntu 14.04 crontab 执行ruby 脚本

参考:[(http://ruby-china.org/topics/9690)]
http://www.linuxidc.com/Linux/2015-02/113836.htm

要添加cron任务,你可以使用称为crontab的命令行工具。
输入下面的命令会创建一个以当前用户运行的新cron任务。

$ crontab -e

如果你想要以其他用户运行cron任务,输入下面的命令。

$ sudo crontab -u <username> -e

你将会看到如下的提示,输入对应的数字选择编辑器

no crontab for jim - using an empty one

Select an editor.  To change later, run 'select-editor'.
  1. /bin/ed
  2. /bin/nano        <---- easiest
  3. /usr/bin/vim.basic
  4. /usr/bin/vim.tiny

然后会看见一个文本编辑框,将任务编写进去

# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
  * * * * * bash /home/jim/my_test.sh
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command

注意书写的格式。

每个cron任务的格式如下。

<分钟> <小时> <日> <月> <星期> <命令>

前5个元素定义了任务的计划,最后一个元素是命令或者脚本的完整路径。

如图

下面是一些cron任务示例。

* * * * * /home/dan/bin/script.sh: 每分钟运行。
0 * * * * /home/dan/bin/script.sh: 每小时运行。
0 0 * * * /home/dan/bin/script.sh: 每天零点运行。
0 9,18 * * * /home/dan/bin/script.sh: 在每天的9AM和6PM运行。
0 9-18 * * * /home/dan/bin/script.sh:9AM到6PM的每个小时运行。
0 9-18 * * 1-5 /home/dan/bin/script.sh: 周一到周五的9AM到6PM每小时运行。
*/10 * * * * /home/dan/bin/script.sh: 每10分钟运行。

保存关闭编辑器,此时,新增的计划任务应该已经激活了。

要查看存在的计划任务,使用下面的命令:

$ crontab -l

建立my_test.sh 脚本格式如下:

#!/usr/bin/
/home/jim/.rvm/rubies/ruby-2.2.0/bin/ruby /home/jim/my_test.rb

脚本文件中不要忘了写上 #!/usr/bin/ 这行来加载环境变量
首先 crontab任务列表里面路径要写全,好像 ~/text.txt 是不支持的。

命令行里直接 ruby ~/xxx.rb 就可以了 可是在 crontab列表里面就不行,要写出 ruby 的完整路径。
使用 which ruby 查出 ruby 的完整路径。

jim@jim-ThinkPad:~$ which ruby
/home/jim/.rvm/rubies/ruby-2.2.0/bin/ruby

快捷键

  • 加粗 Ctrl + B
  • 斜体 Ctrl + I
  • 引用 Ctrl + Q
  • 插入链接 Ctrl + L
  • 插入代码 Ctrl + K
  • 插入图片 Ctrl + G
  • 提升标题 Ctrl + H
  • 有序列表 Ctrl + O
  • 无序列表 Ctrl + U
  • 横线 Ctrl + R
  • 撤销 Ctrl + Z
  • 重做 Ctrl + Y

表格

Markdown Extra 表格语法:

项目价格
Computer$1600
Phone$12
Pipe$1

可以使用冒号来定义对齐方式:

项目价格数量
Computer1600 元5
Phone12 元12
Pipe1 元234
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值