Raspberry - Issue with cron automation - python

I have an issue with cron job on a Raspberry Pi.
I've search the internet and StackOverflow but nothing seems to fix my problem.
I'm new to Linux and think I might have broken something that I don't understant yet.
So : Cron tasks work but not fully.
I have a test script called test2.py writing in a file and printing File created in the console.
When I run this script myself, it works.
When I run this script with cron, it prints the "File created" but won't create the file or write in it.
I watched syslog, cron is running the task and logging in a file. This log file only give me the print output and no other error. File was not created.
Where did I mess this up ?
TLDR : Cron run the task, I can see the script print but not writing in a file.
test2.py :
file = open('testfile.txt','w')
file.write('Hello World')
file.close()
print('File created')
This is the Crontab entry : */1 * * * * /usr/bin/python3 /var/www/test2.py >> /tmp/cron.log 2>&1
Here are the perm on the files :
273281 4.0K -rw-r--r-- 1 pi pi 211 Dec 17 11:32 test2.py
Or, not working either :
273281 4.0K -rwxrwxrwx 1 pi pi 211 Dec 17 11:32 test2.py
Thanks for your help and suggestions.

Tin Nguyen solved this for me.
It was simply a path issue. File is created somewhere. My path was relative and cron directory is not the same as the one I ran the script from.
Thanks a lot.

Related

Starting python script on Google Cloud Compute instance startup

I've been trying to find the best way to start a python script on startup of my cloud compute instance. So far, I haven't gotten it to run. The script does run when manually executed on the instance. I did make the file executable.
This is what I have tried so far:
1)
Add script directly to metadata with key "startup-script". Script starts with:
#! /usr/bin/python3
Followed by the script contents.
Result:
Won't run, doesn't show up in log.
2)
Try to execute local script from metadata with key "startup-script":
#! /usr/bin/bash"
/home/dir/scripts/script.py.
Result:
Won't run, doesn't show up in log.
3)
Point to file located in storage bucket with "startup-script-url".
gs://project.appspot.com/folder/script.py
Result:
Won't run, but shows "Found startup script" in log.
I hope anyone has some insights.
This worked for me:
#! /bin/bash
cat <<EOF > /var/myScriptStackOverflow.py
with open("/var/python_was_here_stack_overflow.txt", "w") as file:
file.write("Looks that the script is executed")
EOF
python3 /var/myScriptStackOverflow.py
The script above is explicit in respect to the paths, but this also works:
#! /usr/bin/python3
with open("/var/python_was_here_stack_overflow.txt", "w") as file:
file.write("Looks that the script is executed this way as well...")
Edit the instance, paste the script above in the Custom metadata with the key startup-script:
Reset the instance
ssh inside the instance to check the results:
ls -la /var | grep -i py
-rw-r--r-- 1 root root 119 Aug 3 17:33 myScriptStackOverflow.py
-rw-r--r-- 1 root root 33 Aug 3 17:33 python_was_here_stack_overflow.txt
cat /var/myScriptStackOverflow.py
with open("/var/python_was_here_stack_overflow.txt", "w") as file:
file.write("Looks that the script is executed")
cat /var/python_was_here_stack_overflow.txt
Looks that the script is executed

cronjob python script output to file

I run a simple python script by crontab in a CentOS 7.x server.
This is the code:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
#MAILTO=root
MAILTO=my_email_address
00 15 * * * root /usr/bin/python ./home/python_devs/run_program.py >> /home/logs/Python_log_`date +\%Y-\%m-\%d_\%H\%M`.log
Unfortunately, it creates the log file, but it is completely blank. If I remove the >> .... the script output is being sent to my email address defined in MAILTO= field.
So, how to save that output into the log file, which I then send by email?
Thank you.
Try this line:
00 15 * * * /usr/bin/python /home/python_devs/run_program.py >> /home/logs/Python_log_`date +\%Y-\%m-\%d_\%H\%M`.log 2>&1
I'm unclear why you had 'root' at the start of the line. Maybe that is a Centos-ism (I've always used debian based systems). But I've removed it since it appears you are trying to run a user file anyway.
Normally (debian) if you want root to run something you put it on the sudo crontab. I also removed the . before the path to the file.
Finally the 2>&1 also redirects stderr into your log. Maybe there is an error happening early and thus nothing is getting written to log, this will catch this case (and is a good practice anyway if you are trying to log a file this way)

Python script won't play sound as cgi, but via command line

The CGI python script shown below, sound.py, plays a sound through a speaker connected to a Raspberry’s audio output. The sound plays fine when I execute the script from the command line via “python sound.py”. However, when I try to trigger it as a cgi script by invoking it via the Web (http://192.168.1.246/cgi-bin/sound.py), the sound won’t play, and the browser will only display “Hi Hello World!” (which, I suppose, indicates that the server at least recognizes it as a CGI script and executes it without producing errors).
I thought that the issue might have to do with the permissions/ownership of the sound file and tinkered around with those, but that didn’t help (I’m also pasting below information about the ownership and permission of the script itself, and the sound file).
Thanks a lot for helping!
Marc.
-rwxrwxrwx 1 root root 441 Jul 10 23:23 sound.py
-rwxr-xr-x 1 root root 29812 Jul 9 22:58 cardinal-short.mp3
Script (sound.py):
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# enable debugging
import cgitb
cgitb.enable()
import pygame
name = "cardinal-short"
pygame.mixer.init()
pygame.mixer.music.load("/var/www/html/cgi-bin/cardinal-short.mp3")
pygame.mixer.music.play()
pygame.mixer.music.set_volume(1.0)
while (pygame.mixer.music.get_busy() == True):
continue
pygame.mixer.quit()
print "Content-Type: text/plain;charset=utf-8"
print
print "Hi Hello World!"
I was able to fix this via:
sudo usermod -aG audio www-data
and then restarting apache.
The problem was that, and I quote from elsewhere on the web:
the sound devices (in /dev/snd/) are only accessible to members of group "audio". pi is a member of that group, and www-data is not.

Script not running from crontab but running from terminal?

I have a python script /var/www/html/dummy/a.py under Ubuntu 14.04 server 32bit
My script updates my status on twitter every time the server is rebooted
The script in question has the right permissions for execution.
My crontab is as follows:
reboot / usr / bin / python /var/www/html/dummy/a.py > / dev / null
but ... but ... my script does not run
I see the following logs:
Mar 12 09:05:53 dumm cron[890]: (CRON) INFO (Running #reboot jobs)
Mar 12 09:05:53 dumm CRON[904]: (gri) CMD (python /var/www/html/dummy/bb.py >/dev/null)
Can someone help me to know because it does not run?
Let me know if I need to provide more information?
EDIT:
I managed to solve this myself - it is necessary in the script to insert the full path !
SOLVED I managed to solve this myself - it is necessary in the script to insert the full path !

Using Crontab with Python

I'm trying to schedule a python script to run every minute or every hour.
What I did so far :
crontab -e
In the crontab file I added
* * * * * /usr/bin/python /path/to/script/script.py
After I save the file I get the message
crontab: installing new crontab
However crontab is not running the script.
Any ideas?
Thanks,
Diez
I found the problem, I will put here the solution maybe it helps someone.
I tried putting in crontab -e * * * * * /usr/bin/python /path/to/script/script.py >>/tmp/script.out 2>&1 as Rafal suggested however the output was blank.
I read a lot about crontab last night and I found out that if you use a script with crontab and you write your output in a certain file you will need to modify your script and input the exact path for the file. If you don't put the full path the script will work when you run it manually. The script will still work if you run it with crontab but it won't know where to place its output.
So modifying
with open('output.txt', 'a') as f
with
with open('/path/to/file/output.txt', 'a') as f
did the job for me.
Thanks,
Diez
Depending on the script (e.g. scripts which prints a message to the console) you won't be able to distinguish whether the script is running or not.
You can assure that crontab is running correctly by typing in the console:
tail -f /var/log/syslog

Categories

Resources