Question is about sudo&cron I have 2 servers:
srv0
- I auth by .ppk and login name
- I run by hand simple .py script with sudo.
- Script runs command to webservice and prints to file time+returned datas(tuple).
It also runs well by cron, which was the aim
srv1 diff
- I auth by root password
- I can run script by hand without sudo and it works well
- Cron runs it well, but prints to file just time. It didn't execute command... It sends empy body message to /var/spool/mail/root
Extras:
- /home/monitors, 2 files(.py, log) are 777 at both servers
whats the problem with cron at srv1? Maybe in fileOpen?
import string
import commands
import time
namesStr ='currtime;wrs-checklist-create;wrs-checklist-delete;wrs-checklist-getall;wrs-checklist-getone;wrs-checklist-update;wrs-photo-queue;wrs-shop-getall;wrs-template-getall;wrs-xauth-authenticate;'
getmess = commands.getstatusoutput('rabbitmqctl list_queues messages')
getmess = list(getmess[1].split('\n'))
messStr = ''
nixtime = str(int(time.time()))
for mess in getmess:
messStr += mess + ';'
logQue = open('/home/monitors/logQue', 'a')
print >> logQue, nixtime + messStr
logQue.close()
In the crontab, before you command, add . $HOME/.bash_profile. For example:
0 5 * * * . $HOME/.profile; /path/to/command/to/run
Cron knows nothing about your shell; it is started by the system, so it has a minimal environment. If you want anything, you need to have that brought in yourself.
OR
just change line
getmess = commands.getstatusoutput('rabbitmqctl list_queues messages')
to
getmess = commands.getstatusoutput('/usr/sbin/rabbitmqctl list_queues messages')
which rabbitmqctl helps you find path to rabbitmqctl
PS
Sorry for my English (: