Obj-C:系统(“echo -e ...”)和没有邮件的 cron
在一个小型备份实用程序中,我尝试使用 system() 写入 Objective-C 中的用户 cron 表。
现在我正在这样做:
NSString *croncmd = [NSString stringWithFormat:@"echo -e 'MAILTO=\"\" \n*/1 * * * * \"%@/Library/Application Support/LBKP/cron/croncall\" %@ > /dev/null 2>&1' | crontab" , NSHomeDirectory(), backup_id ];
system([croncmd UTF8String]);
如您所见,首先我只是尝试使用 > /dev/null 2>&1
在出现错误时删除邮件...但在 OSX 中似乎它根本不起作用,我仍然收到电子邮件。
然后在线阅读一些信息,我刚刚在 cron 表的开头遇到了使用 MAILTO=""
来丢弃所有输出。
问题是我需要将“新行”回显到 crontab 文件,通常可以使用终端中的 -e
选项和 \n
字符来制作,但是如果我从我的程序中执行此操作,我会在控制台中得到以下内容:
17/09/11 16:32:52,590 [0x0-0x3a03a].com.home.LBKP: "-":0:糟糕的一分钟
有什么问题吗? :S 我怎样才能摆脱这个错误或只是输出?
谢谢!
In a small backup utility, I'm trying to use system() to write to the user cron table in objective-c.
Now I'm doing this:
NSString *croncmd = [NSString stringWithFormat:@"echo -e 'MAILTO=\"\" \n*/1 * * * * \"%@/Library/Application Support/LBKP/cron/croncall\" %@ > /dev/null 2>&1' | crontab" , NSHomeDirectory(), backup_id ];
system([croncmd UTF8String]);
As you can see, first I simply tried to use > /dev/null 2>&1
to get rid of the mails in case of error... but in OSX seems like it doesn't work at all and I still get emails.
Then reading some info online I just come across the usage of MAILTO=""
at the start of the cron table to discard all the outputs.
The problem is that I need to echo a "new line" to the crontab file witch can usually be made using the -e
option and \n
char in the terminal, but if I do it from my program I get this in the console:
17/09/11 16:32:52,590 [0x0-0x3a03a].com.home.LBKP: "-":0: bad minute
What's the problem? :S How can I get rid of this error or just the output?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚发现问题是在我添加引号和其他东西来制作 NSString 时引起的...无论如何,我发现最安全的方法是:
第二个命令语法
(crontab -l; echo
MAILTO="" 条目都不会被删除。> 列出 cron 表上的可用信息,然后将其和新命令回显给 cron。这样,无论如何, 这不是向 crontab 添加内容的最佳方法我在这里发布一个新问题: Obj-C:向 crontab 添加作业的最佳方法是什么?
希望这对某人有用!
I just discovered that the problem was caused when I added quotes and other things to make the NSString... Anyway I found that the safest way to do this is:
The second command syntax
(crontab -l; echo
lists the available info on the cron table and then echoes it and the new command back to the cron. This way the previousMAILTO=""
entry will not be deleted.Anyway, and specially because I know this is not the best way to add stuff to the crontab I'm posting a new question here: Obj-C: What is the best way to add jobs to crontab?.
Hope this can me useful to someone!