Edit crontab programmatically and force the daemon to refresh

隐身守侯 提交于 2019-12-07 20:33:25

No, there isn't. If you know it's VixieCron, then update the timestamp of the directory. Otherwise, you might be able to fake it: Set the env variable EDITOR before you invoke crontab -e (which should bring up an editor for the crontab).

The idea is to set the editor to some program which makes the change. crontab -e will wait for the editor to terminate and reread the file and tell cron that something has changed.

If you have an old version of cron, though, you must still restart it. But I doubt that you can find such ancient versions on anything that runs Linux or Mac OS X.

Sane modern linux distros have /etc/cron.d/ directory where you can put your crontab entry as a separate file. If I recall correctly the new cronfile gets read within 2 minutes of being written

Thank you very much gnibbler and Aaron. I just went through the source code for both 'whenever' and 'cronedit' gems for Ruby. Both of them does a 'crontab -' which replaces the existing cron file with the updated cron entries. That means contrary to my original question, these libraries make use of the standard crontab tool which in turn would do whatever specifics necessary to refresh the daemon.

I think the best platform-independent, and cron (dillon, vixie, mcron etc.) agnostic way to refresh the daemon is to always use the associated crontab utility. Even when editing programmatically, the user-program must pass the new cron file to the crontab utility which would do whatever is required by that specific platform/cron.

@Mark, thank you for the comments. However, I'm trying to provide a simple web frontend that the user can customize through the www just like editing the crontab file directly.

Thank you so much for your responses!

If this is that important, why not use a different lever? In other words, there are two options:

crontab 1: /do/some/very/specific/thing with specific parameters that i need to change etc.

crontab 2: /do/what/needs/to/be/done

In #2, the job is responsible for determining at run time what needs to be done and doing it. With this design, the notion of needing to understand how crontab works on some detailed level is irrelevant.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!