我想为安装为OpenPKG RPM软件包的应用程序更新非root用户的crontab条目。
目前我在.spec文件的%post部分有这个:
# # Set up the 'app' user's crontab. # Marker lines are used to separate content from different packages: # #Begin of App package # # ... # #End of App package # Replace any possibly existing content between these lines and insert the # content of the installed new file CRONTAB=/var/spool/cron/crontabs/%{V_user} if [ -f $CRONTAB ]; then begin=`head -1 %{V_instdir}/etc/crontab` end=`tail -1 %{V_instdir}/etc/crontab` if [ -z "$begin" ] || [ -z "$end" ]; then echo "Error: Start or end delimiter line is empty. Check '%{V_instdir}/etc/crontab'" exit 1 fi sed -e "/^$begin/,/^$end/d" $CRONTAB > $CRONTAB.tmp cat %{V_instdir}/etc/crontab >> $CRONTAB.tmp mv $CRONTAB.tmp $CRONTAB else cp %{V_instdir}/etc/crontab $CRONTAB fi chown root:sys $CRONTAB chmod 600 $CRONTAB
这不起作用:文件已经正确创build,但是cron不会select更改。 我想不允许直接编辑/var/spool/cron的文件。
什么是正确的方式来编辑crontab?
crontab手册页没有提到从文件加载用户的crontab的方法。 它不接受标准input。 cron守护进程重新读取crontab文件吗? 或者我应该用su
su %{V_user} -c "crontab -l > $tmpfile" # Make the changes su %{V_user} -c "crontab $tmpfile"
如果目标用户没有权限编辑他自己的crontab文件,这不会失败吗?
操作系统是Solaris 10.我没有root权限。 其他人必须安装我创build的RPM软件包。
不幸的是,cron(1M)不接受任何信号重读crontabs。 crontab(1)工具与cron通信的方式是通过进程间通信(请参阅源代码 )。 这就是说,看来crontab可能是你可以用来修改用户的crontab的最好的工具。 你可以编写一个脚本来添加/删除/修改crontab并使用它,如下所示:
EDITOR=<your script> crontab -e <user>
该脚本采用一个参数,该文件的名称包含该用户的crontab的副本,处理该文件,然后退出,返回代码0. crontab然后将通知cron用户的crontab已更改。 如果你最终得到一个空的crontab,你必须改变策略和使用
crontab -r <user>
代替。 有点烦人
另一种可能是在你修改后重启cron
svcadm restart cron
但是这需要su或者至less是solaris.smf.manage.cron的权利。