【问题标题】:Kill a PM2 Cron Job automatically after 72 hours?72 小时后自动终止 PM2 Cron 作业?
【发布时间】:2021-12-22 02:08:39
【问题描述】:

我有一个 PM2,它有一个每 5 分钟运行一次的 Cron 作业

pm2 start shoot-msg.sh --name shoot-messages -i 4 --cron "*/5 * * * *"

如何让 PM2 在 72 小时后退出并终止 cron 作业?

意思是我不想自己做pm2 delete all...

【问题讨论】:

  • 这台 PM2 用在什么地方? windows/linux?
  • @deechris27: Linux , Ubuntu
  • 它对您的要求有帮助吗?

标签: node.js cron pm2 cron-task


【解决方案1】:

您需要的是 Ubuntu 中的任务调度程序。 72 小时后使用 Crontab 杀死进程。

  1. 安装 crontab

    sudo apt install cron

  2. 设置为在后台运行

    sudo systemctl enable cron

  3. 编辑 crontab

    crontab -e

  4. 选择编辑器

    1. /bin/nano
    2. /usr/bin/vim.basic
    3. /usr/bin/vim.tiny
    4. /bin/ed

ENTER 选择默认nano。

编辑器会显示如下,

# 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/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
  1. 滚动到编辑器底部并使用 cron 添加 kill 命令

    0 */72 * * * /bin/node /bin/pm2 delete all

只需将这些值(/bin/node、/bin/pm2)替换为 /etc/init.d/pm2-init.sh 文件中的值即可。

  1. 保存CTRL+O并退出CTRL+X

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 2020-01-01
    • 2016-10-31
    • 2014-05-25
    相关资源
    最近更新 更多