我是设置 Cron 作业的新手。
我有一个用于我的应用程序 python 的前缀目录,/usr/local/lib/python2.7/dist-packages/prewikka
并且我已经为我的应用程序 python 创建了新环境PYTHONPATH=$prefix/lib/python2.7/dist-packages/prewikka:$prefix/bin/prewikka-httpd
我已经手动创建了一个 MySQL 数据库:
mysql> DESC Prewikka_Crontab;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id | varchar(255) | YES | | NULL | |
| name | varchar(255) | NO | PRI | NULL | |
| userid | varchar(255) | YES | | NULL | |
| ext_type | varchar(255) | NO | | NULL | |
| ext_id | varchar(255) | YES | | NULL | |
| base | varchar(255) | NO | | NULL | |
| runcnt | varchar(255) | YES | | NULL | |
| schedule | varchar(255) | NO | | NULL | |
| enabled | tinyint(11) | YES | | 1 | |
| error | varchar(255) | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+
10 rows in set (0.00 sec)
如果我启动prewikka-crontab
,则不会出现任何输出。当我启动时prewikka-httpd
,我总是得到输出:
...
File "/usr/local/lib/python2.7/dist-packages/prewikka/history.py", line 102, in <module>
crontab.schedule("search_history", N_("Search history deletion"), "0 * * * *", _regfunc=history._history_cron, enabled=True)
File "/usr/local/lib/python2.7/dist-packages/prewikka/crontab.py", line 240, in schedule
self._init_system_job(ext_type, name, schedule, enabled, _regfunc)
File "/usr/local/lib/python2.7/dist-packages/prewikka/database.py", line 91, in inner
ret = func(self, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/prewikka/crontab.py", line 154, in _init_system_job
self.add(name, schedule, ext_type=ext_type, enabled=enabled)
File "/usr/local/lib/python2.7/dist-packages/prewikka/crontab.py", line 219, in add
return self.update(None, name=name, schedule=schedule, user=user, ext_type=ext_type, ext_id=ext_id, enabled=enabled)
File "/usr/local/lib/python2.7/dist-packages/prewikka/crontab.py", line 209, in update
return env.db.getLastInsertIdent()
RuntimeError: could not retrieve last insert ID
因为我的客户端到服务器端没有事件调度程序,如何使用 Cron 作业来启动它?
提前致谢。
答案1
我明白了关联. 基于该链接上的“删除心跳”部分。
步骤1编写.sh
脚本:
#!/bin/sh
set -e
DB_TYPE="mysql"
DB_HOST="localhost"
DB_USER="xxxxxx"
DB_PASS="xxxxxx"
KEEP_INTERVAL="2 month"
DATE=$(date -d "now - $KEEP_INTERVAL" +%Y-%m-%d)
preludedb-admin delete heartbeat --criteria "heartbeat.create_time <= $DATE" "type=$DB_TYPE host=$DB_HOST user=$DB_USER pass=$DB_PASS"
并使.sh
之可执行。
第2步制定一个 cron 任务
sudo crontab -e
添加事件调度程序,
*/2 * * * * /usr/local/lib/python2.7/dist-packages/prewikka/crontab.py
*/2 * * * * /path/to/sripts/prewikka.sh
重新开始使用sudo init 6
。
希望这可以帮助。