如何将 cron 转换为守护进程

发布于 2024-12-18 04:17:46 字数 532 浏览 2 评论 0原文

我有一个 cron shell 脚本,它调用存储过程来执行某些操作。我想知道如何将该 cron 更改为守护进程。

这就是我所拥有的。

    #!/bin/ksh
    # @(#)abc.sh    


    . /somedir/env/some_profile
    JOBNAME=`basename $0`
    LOG_DIR=$PROJHOME/log
    LOG_FILE=$LOG_DIR/process_abc.log

    function usage
    {
      echo "${JOBNAME}: Usage: ${JOBNAME}"
      echo "Exiting..."
      echo "process_abc ended ***UNSUCCESSFULLY*** at `date`" | tee -a $LOG_FILE
      echo ""  | tee -a $LOG_FILE
      exit 1
    }

你能帮忙吗?我们将不胜感激您的帮助。

谢谢

I have a cron shell script which calls a stored procedure to do some operation. I want to know how can I change that cron to a daemon.

Here's what I have.

    #!/bin/ksh
    # @(#)abc.sh    


    . /somedir/env/some_profile
    JOBNAME=`basename $0`
    LOG_DIR=$PROJHOME/log
    LOG_FILE=$LOG_DIR/process_abc.log

    function usage
    {
      echo "${JOBNAME}: Usage: ${JOBNAME}"
      echo "Exiting..."
      echo "process_abc ended ***UNSUCCESSFULLY*** at `date`" | tee -a $LOG_FILE
      echo ""  | tee -a $LOG_FILE
      exit 1
    }

Can you please help. Your help will be appreciated.

Thanks

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

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

发布评论

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

评论(3

屌丝范 2024-12-25 04:17:46

你可以从 bash 运行它

SLEEP_INTERVAL=3600
(echo $ > /var/run/script-runner.sh.pid && while :; do script.sh ; sleep $SLEEP_INTERVAL ; done) &

,或者你可以在脚本中执行相同的循环,

trap "function_that_runs_everything" HUP
echo $ > /var/run/script.sh.pid
while :; do
    function_that_runs_everything
    sleep $SLEEP_INTERVAL #or sleep forever just waiting for SIGHUP
done

这样你也可以从外部强制执行内部函数,

kill -1 `cat /var/run/script.sh.pid`

我也建议重写:

if [ `/usr/ucb/ps -auxww | grep $JOBNAME | grep "sh -c" | grep -v grep  | wc -l` -gt 1 ]

使用 $$ (当前 shell 的 pid)并创建 pid文件

pseudocode:
PIDFILE=/var/run/`basename $0`.pid
if $PIDFILE exists
    log and exit
else
    echo $ > $PIDFILE

...
run
...
rm -f $PIDFILE

(不要忘记删除 pid 文件@失败退出)

you can just run it from bash

SLEEP_INTERVAL=3600
(echo $ > /var/run/script-runner.sh.pid && while :; do script.sh ; sleep $SLEEP_INTERVAL ; done) &

or you can do the same loop inside the script

trap "function_that_runs_everything" HUP
echo $ > /var/run/script.sh.pid
while :; do
    function_that_runs_everything
    sleep $SLEEP_INTERVAL #or sleep forever just waiting for SIGHUP
done

this way you can also force the execution of the internal function from the outside

kill -1 `cat /var/run/script.sh.pid`

also i would suggest rewriting:

if [ `/usr/ucb/ps -auxww | grep $JOBNAME | grep "sh -c" | grep -v grep  | wc -l` -gt 1 ]

to use $$ (current shell's pid) and create pid file

pseudocode:
PIDFILE=/var/run/`basename $0`.pid
if $PIDFILE exists
    log and exit
else
    echo $ > $PIDFILE

...
run
...
rm -f $PIDFILE

(dont forget to remove the pid file @ failure exits)

枉心 2024-12-25 04:17:46

为什么不使用 Fat Controller 来守护它呢?

fat-controller.sourceforge.net

Why don't you just daemonise it using The Fat Controller?

fat-controller.sourceforge.net

寂寞美少年 2024-12-25 04:17:46

让我们考虑一下守护进程的作用。它坐下来等待事件,然后响应它们。这与 cron 作业不同,后者只是根据计划运行。

制作一些将运行的东西,等待某事发生,在它发生时执行某事,然后继续等待某事再次发生,这是非常容易的。您真正需要的只是现有 shell 脚本的包装器。包装器需要识别事件的条件,并启动您原本在 cron 中启动的脚本。

包装器可以像 shell 脚本一样简单,由 cron 作业在“@reboot”特殊字符串上运行(man 5 crontab 了解详细信息)。直接运行包装器脚本,或者在 GNU Screen 内运行,以便您将来可以附加到它并查看它的运行情况(如果您没有包含错误处理)。为了聪明起见,它还应该避免一次多次运行现有脚本(即给它一个锁定文件),尽管您已经在上面的 ksh 脚本中进行了处理。

您想要识别一个将触发脚本运行的事件,而不是上面jackdoe 的无休止的sleep 循环。例如,当使用 vsftpd 上传名为“somefile”的文件时是否应该运行它?然后:

#!/bin/sh
tail -F /var/log/vsftpd.log | while read line; do
  case "$line" in
    *UPLOAD*/somefile",*)
      /path/to/abc.sh
      ;;
  esac
done

这是一个半途而废的解决方案,不包括启动/停止功能,但它会让你继续前进。

实际上,这个包装器应该负责锁定以避免多次启动 abc.sh,并且应该维护自己的 pid 文件,以便它可以被 /etc/init.d/ (Linux) 或 /usr/local/ 中的处理程序杀死etc/rc.d/(FreeBSD)。

Let's think about what a daemon does. It sits and waits for EVENTS, then responds to them. This is different from a cron job, which simply runs according to a schedule.

It's very easy to make something that will run, wait for something to happen, do something when it happens, then continue waiting for something to happen again. All you really need is a wrapper for your existing shell script. The wrapper needs to recognize the conditions of an event, and launch the script you'd otherwise launch in cron.

The wrapper can be as simple as a shell script that gets run by a cron job on the "@reboot" special string (man 5 crontab for details). Run the wrapper script directly, or inside GNU Screen so you can attach to it in the future and see how it's doing (if you didn't include error handling). To be smart, it should also avoid running your existing script more than once at a time (i.e. give it a lock file), though you're already sort of handling that in your ksh script, above.

Rather than jackdoe's unending loop with sleep, above, you want to recognize an event that will trigger your script to run. For example, should it be run when a file named "somefile" gets uploaded using vsftpd? Then:

#!/bin/sh
tail -F /var/log/vsftpd.log | while read line; do
  case "$line" in
    *UPLOAD*/somefile",*)
      /path/to/abc.sh
      ;;
  esac
done

It's a hackish, half-assed solution that doesn't include start/stop functionality, but it'll get you going.

Really, this wrapper should take care of locking to avoid launching abc.sh multiple times, and should maintain its own pid file so it can be killed by a handler in /etc/init.d/ (Linux) or /usr/local/etc/rc.d/ (FreeBSD).

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