写下面文本到文件 /home/sftp/check_frps.sh

#!/bin/bash

# author: hellodk

# time: 2021-05-28 16:14:26

# script feature description: check if frps is running, if not, start it

function check_frps(){

  time3=$(date "+%Y-%m-%d %H:%M:%S")

  frpsCount=`ps aux | grep "/usr/local/frp/frps -c /usr/local/frp/frps.ini" |grep -v grep |wc -l`

  if [ 0 == $frpsCount ]; then

      echo $time3 "current running frps process count: "$frpsCount ", meaning frps daemon is dead, now start it" >> /home/sftp/check_frps.log
      nohup /usr/bin/systemctl start frp >> /home/sftp/check_frps.log 2>&1 &
   else
      echo $time3 "current running frps process count: "$frpsCount ", and no need to start frps" >> /home/sftp/check_frps.log

  fi

}

check_frps

脚本是没有问题的,使用bash执行也是正常,日志打印也正常,但就是在我的那台硬件配置比较差的vps上总是无法使用 crontab 保存执行

每次使用 crontab -e 新增一行然后保存退出,成功提示crontab: installing new crontab 但是过了一段时间后发现这行就不见了……

脚本自然也就没有按照预期执行。

由于这一台主机挂了京东薅羊毛的脚本,后续发现了问题所在。

在狗东薅羊毛控制面板(web端)直接编辑了这个cron list,成功新增一行,web的提示是成功修改了 crontab.list 文件,后面通过 find 命令找到了

# find / -name crontab.list
/opt/jd/config/crontab.list

我应该直接修改这个文件,新增我的cron计划。

需要额外重点关注:crontab -e 命令编辑的文件是 /var/spool/cron 目录下的具体的用户名文件 比如 root, 具体文件是 /var/spool/cron/root

0 0-23/1 * * * bash /home/sftp/check_frps.sh, 含义是每隔一小时执行一次这个 shell 脚本 或者 0 */1 * * * bash /home/sftp/check_frps.sh 这两个命令都代表每隔一小时执行一次(也就是每小时检查一次frps进程是否存在)

将这行新增到 /opt/jd/config/crontab.list 文件末尾保存退出即可。后续查看脚本发现稳定运行~

看看日志

# tail -n 15 /home/sftp/check_frps.log
2021-06-06 20:00:01 current running frps process count: 1 , and no need to start frps
2021-06-06 21:00:02 current running frps process count: 1 , and no need to start frps
2021-06-06 22:00:01 current running frps process count: 1 , and no need to start frps
2021-06-06 23:00:01 current running frps process count: 1 , and no need to start frps
2021-06-07 00:00:04 current running frps process count: 1 , and no need to start frps
2021-06-07 01:00:02 current running frps process count: 0 , meaning frps daemon is dead, now start it
2021-06-07 02:00:02 current running frps process count: 1 , and no need to start frps
2021-06-07 03:00:02 current running frps process count: 1 , and no need to start frps
2021-06-07 04:00:02 current running frps process count: 1 , and no need to start frps
2021-06-07 05:00:01 current running frps process count: 1 , and no need to start frps
2021-06-07 06:00:01 current running frps process count: 1 , and no need to start frps
2021-06-07 07:00:02 current running frps process count: 1 , and no need to start frps
2021-06-07 08:00:02 current running frps process count: 1 , and no need to start frps
2021-06-07 09:00:01 current running frps process count: 1 , and no need to start frps
2021-06-07 10:00:02 current running frps process count: 1 , and no need to start frps

问题解决了。end.