I am trying to run a Python script called probemon.py in cron (crontab -e) and cannot get it to work. The path to the file is /home/pi/probemon.py and this must be run with the sudo command usually (i.e. sudo python probemon.py). I have tried many methods, including:
52 23 * * * sudo python /home/pi/probemon/probemon.py
and yet nothing works. Any ideas about how to do this?
Try adding the command to the sudo-users crontab instead of trying to run it with a sudo from a normal users crontab. I think what happends is that "sudo ..." will ask for the sudo-password, and wait in this stage forever, since noone is providing one.
Related
Please note, I'm NOT asking about the user running it but rather did he run it as sudo command or not
I have a python script, which I want to obligate the user to run it like this:
sudo python3 script.py
and not like this:
python3 script.py
How can I do verify in run time if it was run correctly or not (if so end program using sys.exit())?
Check out this topic:
What is the best way for checking if the user of a script has root-like privileges?
It has a bunch of solutions that you might find helpful.
I have a python script which has to run once every 3 minutes. The script runs a process can run for more than 10 minutes though. And hence I need to make sure that if it's already running it shouldn't run. I need to do this without any database interference.
The approach I have used it is having a cron via the following command in the crontab.
*/3 * * * * sudo ps aux|grep -v grep|grep "python XMLProcessor.py"
|| cd /home/ubuntu/git/perpule-python-subscriber; sudo python XMLProcessor.py
It runs smoothly. But the problem here is, once in a while, even after the process ends, the command sudo ps aux|grep -v grep|grep "python XMLProcessor.py" still gives an output because of which the python script doesn't run.
Please suggest me a better approach or rectify the one I'm using. All the suggestions are appreciated.
The approach you are using has some problems as ps can report "unexpected things". Are you sure that your python program has a unique name? Could it be that there are race conditions (it could run the program twice)?
A typical way of doing this is to touch a file at the start of the process and remove it and the end of it:
if [ ! -f .working ]; then
touch .working && \
python do_something.py && \
rm .working
fi
Then you can check if it's working by checking for that file. However, there are multiple problems with this approach: what happens if the process crash? should you remove the touched file? is it possible to remove it for every possible crash?. Then you need to add timeouts, and it starts getting complicated.
The proper and safer solution then is to use some sort of server or tool that checks that your job is being run, and if not it runs it. I have used luigi to do something similar and it integrates quite well with python code, so you could give it a try.
The script create some files in directories which need sudo permissions and executes few command that also need sudo privillage.
I want to execute that script giving sudo privillage.
Is there any way to do that ?
I am trying to execute it with python-shell module as well as spawn child process.
I never got any answer on it, So I researched it on my own. The besy way to run any shell command or script is by using node-cmd moudle. It works soo bright .
Just run the node script with sudo privillege, and you are good to go .
It's bad practice to give sudo, as a hacker could do anything if there is any security issues. You could give the user witch runs the web server the permission to do the task your task is intending to do.
In general try to avoid root whenever you can.
I'm trying to automate a test in python 2.7 (in eclipse on linux ubuntu 12.04).
The test checks configurations on another pc, so I'm using fabric for the ssh connection.
I need to execute a script:
run("cd somepath && ./execute_script.sh")
The problem is that my script needs a sudo to run, but changing the command to this:
sudo("cd somepath && ./execute_script.sh")
does not work since "cd" doesn't work in combination with sudo.
I also cannot split the command in two parts, because that would create 2 shells, and the second one would forget the path I've been going to in the first one.
If I do it like this:
run("cd somepath && sudo ./execute_script.sh")
the test wouldn't work completely automatic since you would have to enter the password at a time.
Is there a way to some sort of combine run() and sudo()?
How about:
from fabric.api import cd,sudo
with cd('somepath'):
sudo('./execute_script.sh')
I am writing a script in python3 for Ubuntu that should be executed all X Minutes and should automatic start after logging in. Therefore I want to create a daemon (is it the right solution for that?) but I haven't found any modules / examples for python3, just for python 2.X. Do you know something what I can work with?
Thank you,
I would simply make the script, and have it somewhere, and then add a line to the crontab of the user who you want to run the script. This may be the root.
sudo crontab -e
To start the editor of the crontab
X * * * * /usr/bin/python /path/to/the/script
This way the script will be executed every X minutes. No need to daemonize, no need to make your own timer in the script.
Suppose for python script name is monitor. use following steps:
copy monitor script in /usr/local/bin/ (not necessary)
Also add a copy in /etc/init.d/
Then execute following command to make it executable
sudo -S chmod "a+x" "/etc/init.d/monitor"
At last run update.rc command
sudo -S update-rc.d "monitor" "defaults" "98"
this will execute you monitor whenever you login for all tty.