I have this .sh which starts a python file. This python file generates a .txt when started via the commandline with sudo but doesn't when started via the .sh
Why doesn't the pyhton file give me a .txt when started with the cron and .sh?
When I use su -c "python /var/www/html/readdht.py > /var/www/html/dhtdata.txt" 2>&1 >/dev/null, .sh gives me output, but omits the newlines, so I get one big string.
The python file creates a .txt correctly when started from the commandline with sudo python readdht.py.
If the .sh the python file is started with su -c "python /var/www/html/readdht.py no .txt is created
What's going on?
Difficult to answer without more colour on your environment.
Here's how to solve this though: do not redirect your output to /dev/null. Then read in your cron log what happened. It seems very likely that your script fails, and therefore does not return anything to standard out, so does not create a file.
I highly suspect it is because you are using a python module, or python version or python path that is loaded in your bashrc. Crontab does not execute your bashrc, it's an independent environment, so you cannot assume that a script that runs correctly when you manually launch will work in your cron.
Try sourcing your bashrc in your cron task, and it's very likely to solve your problem.
Related
I wrote some code on my laptop and now I am trying to run this code. But it give me the output : Permission denied
Usually that should only happen if a different user would try to access it. But I am the user who wrote it and is trying to run it now. I feel like this will be an easy issue to resolve but any help would be nice! Thanks.
I wrote and saved the code in IDLE python. Once I finished I tried running it in my Terminal (I have the macOS Mojave Version 10.14.5) but then I ran into the issue.
Evandros-MBP:~ evandro$ ./Desktop/FunCodes/pwd.py
This is how I tried to call up the code. I don't see any issues here.
Instead of calling the script with
./Desktop/FunCodes/pwd.py
try using
python ./Desktop/FunCodes/pwd.py
or also
python3 ./Desktop/FunCodes/pwd.py
if you want to execute it with python3 instead of python2.
Explanation
When you write ./some-file in the terminal (without writing python before the file path), the terminal executes it as a bash file (no matter if the file has a .sh extension or not). Of course, since your file is a python file, this will fail. But even before it fails, since your python file has no permission to be executed, permission will be denied and bash won't even start.
On creation, the file only has read and write permissions. Add a shebang pointing to the Python interpreter on the script's first line, for example #!/usr/bin/env python. Also, your script is not marked as executable, in order to do that you need to use:
chmod +x pwd.py
Otherwise you will only be able to run it with Python, with
python pwd.py
I am a noob, self-motivated programmer, and had been researching methods to use my Python script to run a Powershell file that will copy and image and place the image into Excel.
I've used the subprocess, call, and Popen commands in an attempt to call and run the Powershell program from the Python script, but none has worked.
Some of the examples I found only called different functions of a Powershell script, but when I tried those settings it didn't work for my program. All of the setup for my Powershell has been established so that it can run with my PC, and also runs well when launched independently from Python.
What I would like to ask is if I had, for example, a My_program.py file and a Moving_image.ps1 file. I want to use my .py file to run/execute my .ps1 file, while both programs are located in the same path (C:\Users\Scripts).
What line of code(s), imports, and other program setup's would I need in my Python file to simply run the independent .ps1 file from my Python script?
I don't need the Powershell script to return anything to the Python script. I would like for it to simply run the copy and paste the command I sent it.
Thank you. Any type of guidance that will lead to this program actually functioning properly will be most appreciated!
Here's what worked for me (testing on linux):
python script test.py
from subprocess import call
call(["/opt/microsoft/powershell/6.0.4/pwsh", "./test.ps1"])
powershell script test.ps1
Import-Module '/home/veefu/pwshmodules/testMod'
Write-Output "Hello from test.ps1"
Get-HelloWorld
testMod.psm1 module, stored at /home/veefu/pwshmodules/testMod
function Get-HelloWorld {
Write-Output "Hello World (from Module)"
}
result when running the python script:
Hello from test.ps1
Hello World (from Module)
On windows you'll probably have to provide the complete path, C:\Windows\system32\MicrosoftPowerShell\1.0\powershell.exe and you may have to pass /file .\yourOtherScript.ps1 in the second argument to call
I just set up my first aws server for a personal project I'm doing. I'm running ubuntu linux, and I have a python script that accesses an sqlite database file in order to send email. I have these same files on my own ubuntu machine and the script works fine. I'm having trouble, however, figuring out how to run my script from the terminal in my aws vm. Normally I use idle to run my python script on my linux machine, so I'm trying to figure out how to run it from the terminal and it's giving me some trouble.
I tried
python script.py
which did nothing, so I converted it to an executable, ran it, and got
./script.py: line 1: import: command not found
...and so on
I realized that I had to add
#!/usr/bin/env python3
to my script, so I did that, converted to executable again, and ran it by entering
./script.py
which also did nothing. If the program had run, it would have delivered an email to my inbox. Is there any way I can tell if it's actually trying to run my script? Or am I trying to run it incorrectly?
You can modify the script to add verbose that prints out to the console the status of the script, or if you just want to know whether your script is running in the background, you can check whether the process is active using ps (process name would be the name of the script) :
ps aux | grep "script.py"
Anyways the former is a better practice, since you can exactly know execution flow of your script.
I am new to Rundeck, so I apologize if I ask a question that probably has an obvious answer I'm overlooking.
I've installed Rundeck on my Windows PC. I've got a couple of Python scripts that I want to execute via Rundeck.
The scripts run fine when I execute them manually.
I created a job in Rundeck, created a single step (script file option) to test the python script.
The job failed after six seconds. When I checked the log, it was because it was executing it line by line rather than letting python run it as an entire script.
How do I fix this?
You had to put:
#!/usr/bin/python
or similar, with location to your python binary, as 1st line. To indicate which interpreter to use for whole file.
okay, so I changed the step type to a command rather than script file and it worked.
I guess my understanding of what a script file is was off.
I am trying to run a Python script from cron. I am using crontab to run the command as a user instead of root. My Python script has the shebang at the top #! /usr/bin/env python and I did chmod +x it to make the script executable.
The script does work when running it from a shell but not when using crontab. I did check the /var/log/cron file and saw that the script runs but that absolutely nothing from stdout or stderr prints anywhere.
Finally, I made a small script that prints the date and a string and called that every minute that worked but this other script does not. I am not sure why I am getting these variable results...
Here is my entry in crontab
SHELL=/bin/bash
#min #hour day-of-month month day-of-week command
#-------------------------------------------------------------------------
*/5 * * * * /path/to/script/script.py
Here is the source code of my script that will not run from crontab but will run from the shell it is in when called like so ./script.py. The script is executable after I use the chmod +x command on it, by the way...:
#! /usr/bin/env python
#create a file and write to it
import time
def create_and_write():
with open("py-write-out.out", "a+") as w:
w.write("writing to file. hello from python. :D\n")
w.write("the time is: " + str(time.asctime()) + "\n")
w.write("--------------------------------\n")
def main():
create_and_write()
if __name__ == "__main__":
main()
EDIT
I figured out what was going wrong. I needed to put the absolute file paths in the script otherwise it would write to some directory I had not planned for.
Ok, I guess this thread is still going to help googlers.
I use a workaround to run python scripts with cron jobs. In fact, python scripts need to be handled with delicate care with cron job.
So I let a bash script take care of all of it.I create a bash script which has the command to run the python script. Then I schedule a cron job to run bash script. You can even use a redirector to log the output of bash command executing the python script.For example
#reboot /home/user/auto_delete.sh
auto_delete.sh may contain following lines:-
#!/bin/sh
echo $(date) >> bash_cron_log.txt
/usr/bin/python /home/user/auto_delete.py >> bash_cron_log.txt
So I don' need to worry about Cron Jobs crashing on python scripts.
You can resolve this yourself by following these steps:
Modify the cron as /path/to/script/script.py > /tmp/log 2> &1
Now, let the cron run
Now read the file /tmp/log
You will find out the reason of the issue you are facing, so that you can fix it.
In my experience, the issue is mostly with the environment.
In cron, the env variables are not set. So you may have to explicitly set the env for your script in cron.
I would like to emphasise one more thing. I was trying to run a python script in cron using the same trick (using shell script) and this time it didn't run. It was #reboot cron. So I used redirection in crontab as I mentioned in one of the above comments. And understood the problem.
I was using lot many file handlers in python script and cron runs the script from user's home directory. In that case python could not find the files used for file handlers and will crash.
What I used for troubleshooting was that I created a crontab as below. It would run the start.sh and throw all the stderror or stdoutput to file status.txt and I got error in status.txt saying that the file I used for file handler was not found. That was right because python script was executed by cron from user's home directory and then the script starts searching for files in home directory only.
#reboot /var/www/html/start.sh > /cronStatus/status.txt 2>&1
This will write everything happening during cron execution to status.txt file. You can see the error over there. I will again advice running python scripts using bash scripts for cronjobs. SOLUTION:- Either you use full path for all the files being used in script (which wasn't feasible for me, since I don't want script to be location dependent). Or you execute script from correct directory
So I created my cron as below-
#reboot cd /var/www/html/ && /var/www/html/start.sh
This cron will first change directory to correct location and then start the script. Now I don't have to worry about hardcoding full path for all files in script. Yeah, it may sound being lazy though ;)
And my start.sh looks like-
#!/bin/sh
/usr/bin/python /var/www/html/script.py
Hope it helps
Regards,
Kriss
I had a similar issue but with a little bit different scenario: I had a bash script (run.sh) and a Python script (hello.py). When I typed
sh run.sh
from the script directory, it worked; if I added the sh run.sh command in my crontab, it did not work.
To troubleshoot the issue I added the following line in my run.sh script:
printenv > env.txt
In this way you are able to see the environment variable used when you (or the crontab) run the script. By analyzing the differences between the env.txt generated from the manual run and the crontab run I noticed that the PWD variable was different. In my case I was able to resolve by adding the following line at the top of my .sh script:
cd /PYTHON_SCRIPT_ABSOLUTE_PATH/
Hope this could help you!
Another reason may be that the way we judged executed or not executed is wrong.