如何将 cron 转换为守护进程
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以从 bash 运行它
,或者你可以在脚本中执行相同的循环,
这样你也可以从外部强制执行内部函数,
我也建议重写:
使用 $$ (当前 shell 的 pid)并创建 pid文件
(不要忘记删除 pid 文件@失败退出)
you can just run it from bash
or you can do the same loop inside the script
this way you can also force the execution of the internal function from the outside
also i would suggest rewriting:
to use $$ (current shell's pid) and create pid file
(dont forget to remove the pid file @ failure exits)
为什么不使用 Fat Controller 来守护它呢?
fat-controller.sourceforge.net
Why don't you just daemonise it using The Fat Controller?
fat-controller.sourceforge.net
让我们考虑一下守护进程的作用。它坐下来等待事件,然后响应它们。这与 cron 作业不同,后者只是根据计划运行。
制作一些将运行的东西,等待某事发生,在它发生时执行某事,然后继续等待某事再次发生,这是非常容易的。您真正需要的只是现有 shell 脚本的包装器。包装器需要识别事件的条件,并启动您原本在 cron 中启动的脚本。
包装器可以像 shell 脚本一样简单,由 cron 作业在“@reboot”特殊字符串上运行(
man 5 crontab
了解详细信息)。直接运行包装器脚本,或者在 GNU Screen 内运行,以便您将来可以附加到它并查看它的运行情况(如果您没有包含错误处理)。为了聪明起见,它还应该避免一次多次运行现有脚本(即给它一个锁定文件),尽管您已经在上面的 ksh 脚本中进行了处理。您想要识别一个将触发脚本运行的事件,而不是上面jackdoe 的无休止的sleep 循环。例如,当使用 vsftpd 上传名为“somefile”的文件时是否应该运行它?然后:
这是一个半途而废的解决方案,不包括启动/停止功能,但它会让你继续前进。
实际上,这个包装器应该负责锁定以避免多次启动 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:
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).