I'm asking some help to show notifications using python-crontab, because everything I've tried do not work. The display is not initilised when the script is launched by cron. When I start it manually, that's work.
The codes I've tried:
#!/usr/bin/env python
# coding: utf8
import subprocess
import os
#os.environ.setdefault("XAUTHORITY", "/home/guillaume" + "/.Xauthority")
#os.environ.setdefault('DISPLAY', ':0.0') # do not work
#os.environ['DISPLAY'] = ':0.0' # do not work
print = os.environ
cmd2 = 'notify-send test'
subprocess.call(cmd2, shell=True)
# more code, which is working (using VLC)
cmd3 = "cvlc rtp://232.0.2.183:8200 --sout file/mkv:/path/save/file.mkv" # to download TV's flow
with open("/path/debug_cvlc.log", 'w') as out:
proc = subprocess.Popen(cmd3, stderr=out, shell=True, preexec_fn=os.setsid)
pid = proc.pid # to get the pid
with open("/path/pid.log", "w") as f:
f.write(str(pid)) # to write the pid in a file
# I'm using the pid to stop the download with another cron's task, and to display another notify message.
# Download and stop is working very well, and zenity too. But not notify-send
Thanks
Edit: here are the environment variables I have for this cron's script:
{'LANG': 'fr_FR.UTF-8', 'SHELL': '/bin/sh', 'PWD': '/home/guillaume', 'LOGNAME': 'guillaume', 'PATH': '/usr/bin:/bin', 'HOME': '/home/guillaume', 'DISPLAY': ':0.0'}
Edit2: I'm calling my script in cron like this:
45 9 30 6 * export DISPLAY=:0.0 && python /home/path/script.py > /home/path/debug_cron_on.log 2>&1
I precise I have two screens, so I think DISPLAY:0.0 is the way to display this notify..
But I don't see it.
Edit3: It appears that I've a problem with notify-send, because it's working using zenity:
subprocess.call("zenity --warning --timeout 5 --text='this test is working'", shell=True)
I have notify-send version 0.7.3, and I precise that notify-send is working with the terminal.
Edit4: Next try with python-notify.
import pynotify
pynotify.init("Basic")
n = pynotify.Notification("Title", "TEST")
n.show()
The log file show this: (in french)
Traceback (most recent call last):
File "/home/path/script.py", line 22, in <module>
n.show()
gio.Error: Impossible de se connecter : Connexion refusée
#Translating: Unable to connect : Connection refused
So, I have problem with dbus? what is this?
Solution: Get the DBUS_SESSION_BUS_ADDRESS before creating the cron order:
cron = CronTab()
dbus = os.getenv("DBUS_SESSION_BUS_ADDRESS") # get the dbus
# creating cron
cmd_start = "export DBUS_SESSION_BUS_ADDRESS=" + str(dbus) + " && export DISPLAY=:0.0 && cd /path && python /path/script.py > path/debug_cron.log 2>&1"
job = cron.new(cmd_start)
job = job_start.day.on(self.day_on) # and all the lines to set cron, with hours etc..
cron.write() # write the cron's file
Finally, the cron's line is like that:
20 15 1 7 * export DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-M0JCXXbuhC && export DISPLAY=:0.0 && python script.py
Then the notification is displaying. Problem resolved !! :)
You are calling the cron like
45 9 30 6 * DISPLAY=:0.0 python /home/path/script.py > /home/path/debug_cron_on.log 2>&1
which is incorrect, since you are not exporting the DISPLAY variable, and the subsequent command does not run.
Try this instead
45 9 30 6 * export DISPLAY=:0.0 && cd /home/path/ && python script.py >> debug_cron.log 2>&1
Also, you are setting the DISPLAY variable within your cron job as well, so try if the cron job works without exporting it in the job line
45 9 30 6 * cd /home/path/ && python script.py >> debug_cron.log 2>&1
EDIT
While debugging, run the cron job every minute. Following worked for me:
Cron entry
* * * * * cd /home/user/Desktop/test/send-notify && python script.py
script.py
#!/usr/bin/env python
import subprocess
import os
os.environ.setdefault('DISPLAY', ':0.0')
print os.environ
cmd2 = 'notify-send test'
subprocess.call(cmd2, shell=True)
EDIT 2
Using pynotify, script.py becomes
#!/usr/bin/env python
import pynotify
import os
os.environ.setdefault('DISPLAY', ':0.0')
pynotify.init("Basic")
n = pynotify.Notification("Title", "TEST123")
n.show()
and cron entry becomes
* * * * * cd /home/user/Desktop/test/send-notify && python script.py
EDIT 3
One environment variable DBUS_SESSION_BUS_ADDRESS is missing from the cron environment.
It can be set in this and this fashion
crontab is considered an external host -- it doesn't have permission to write to your display.
Workaround: allow anyone to write to your display. Type this in your shell when you're logged in:
xhost +
Related
I'm on SLES 12 SP5 with python 3 and I met some difficulties for executing a part of my code through cron.
The function in error has to retrieve a file name in another cron file:
def getBackupFileName():
if os.path.exists(BACKUP_CRON_FILE):
cmd='/usr/bin/more /etc/cron.d/backup | cut -d\" \" -f10'
ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = ps.communicate()[0]
sendBackup_logger.info("sortie getBackupNameFile :"+output.decode().strip())
return output.decode().strip()
return null
The /etc/cron.d/backup is:
0 0 * * * root myApp db backup BackupTest.sql.gz
and the cron using the faulty code is:
0 1 * * * root myApp db sendBackup 195.168.1.10
When manually executing myApp db sendBackup 195.168.1.10, this function returns: BackupTest.sql.gz, which is what I was waiting.
During the execution through cron, this function returns:
::::::::::::::
/etc/cron.d/backup
::::::::::::::
BackupTest.sql.gz
Maybe an issue in environment variables? I checked:
myApp: starts with #!/usr/bin/env python3
in crontab environment: PATH=/usr/bin:/usr/sbin:/sbin:/bin:/usr/lib/news/bin
application is launched with root.
Any idea why I got some additional lines in output with cron?
Hi guys I would like to ask for some help with my bash script.
I am running 2 python script inside my bash script and it is working when I'm running it manually but when I'm using cron only the commands in the .sh file is working not on .py
Please take note that I already install necessary utils and packages for python3.
This is the script:
#!/usr/bin/env bash
# list.tmp path directory
fileLoc="/home/ec2-user/PushNotification/Incoming34days/list34days.tmp"
# URL to POST request
refLink='http link'
# Title of Push Notification
title='34th day: Grace Period is about to end'
# curl type
type='Notification'
# curl action_type
actionType='NotificationActivity'
# Get the current date and time
now=$(date '+%b %d %Y %H:%M:%S')
# Message to the user
body="Subscribe to the Philippine Mobile Number plan now to continue receiving calls and texts and sending text messages to the Philippines."
# Logs location
logsLoc="/home/ec2-user/PushNotification/Incoming34days/logs.tmp"
# current number
currentNumLoc="/home/ec2-user/PushNotification/Incoming34days/currentNum.tmp"
echo "[$now] Sending notifications to mobile numbers advising today is the last day of grace period..." > $logsLoc
# Python file to SELECT all id who has 34 days counter
python3 select34days.py
# psql -d $database -t -c "SELECT id FROM svn WHERE current_date - expiry_date::DATE = 4" psql must be setup using .pgpass for postgresql authentication, please indicated database
# name and query list directory. Deleting the last line from list.txt
# This is to read the textfile list.txt line per line
while IFS='' read -r list;
# for list in `cat list.txt`;
do
# curl POST request
response=$(curl --location --request POST $refLink \
--header 'Authorization: Basic YXBwdm5vdXNlcjphcHB2bm9wYXNz' \
--header 'Content-Type: application/json' \
--data-raw '{
"title":"'"$title"'",
"body":"'"$body"'",
"min" :[{"mobileNumber" : "'"$list"'"}],
"type" : "'"$type"'",
"action_type" : "'"$actionType"'"}')
# Echo mobile number
echo "[$now] Mobile Number: $list" >> $logsLoc
# Echo response from curl
echo "Response: '$response'"
echo "[$now] Result: '$response'" >> $logsLoc
# Update the current number of the list
echo $list > $currentNumLoc
echo "[$now] Updating $list status into EXPIRED" >> $logsLoc
# Updating status into EXPIRED
python3 updateQuery34.py
done < "$fileLoc"
# end of script
The select34days.py and updateQuery34.py is not running.
I have a log.tmp to check regarding this situation and only displaying commands inside my .sh file
Inside my cron are
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/bin:/usr/bin
MAILTO=root
Your PATH looks wrong:
PATH=/sbin:/bin:/usr/bin:/usr/bin
This includes /usr/bin twice which isn't necessary, but hints that something else should have been there.
Depending on how you've installed it, python might be in /usr/bin/ or /usr/local/bin or even somewhere in /opt.
At the commandline you can find python's directory using:
dirname $(which python3)
This directory needs to be added to your path in your crontab.
Just declare the specific directory with script name (eg. /etc/test.sh)every time you are using bash scripts and adding it to a cron job since the cron doesn't know where is the specific script within the the server.
I want to make crontab execute python script every minute, but It seems like crontab does not execute script at all.
This is python file where I made crontab job.
schedule.py
from crontab import CronTab
def main():
my_cron = CronTab(user='jelly')
cmd = '.venv/bin/python Users/jelly/PycharmProjects/test/writeDate.py'
job = my_cron.new(cmd, comment='test')
job.minute.every(1)
my_cron.write()
print(my_cron.render())
if __name__ == "__main__":
main()
This is message when I run schdule.py
* * * * * .venv/bin/python Users/hyun/PycharmProjects/test/writeDate.py # test
This is script supposed to be executed
import datetime
with open('dateInfo.txt', 'a') as outFile:
outFile.write('\n' + str(datetime.datetime.now()))
When I go into terminal and typed command line
ps aux | grep cron
I could see messages like this
root 47903 0.0 0.0 2460724 1696 ?? Ss 6:28PM 0:00.31 /usr/sbin/cron
jelly 71389 0.0 0.0 2432804 1380 s003 R+ 12:00PM 0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn cron
This message sometimes shows R+ and sometimes shoes S+, so I assumed that crontab is running, but I can't find any new txt files that supposed to be created in directory. I wonder which part went wrong and how can I fix it.
Problem lies in path to python. Cron runs with limited environment, so it does not know where is .venv/bin/python and Users/jelly/PycharmProjects/test/writeDate.py
You must use absoulte paths
I'm having some difficulty using these three technologies together.
Cron entry:
* * * * * /usr/bin/python /path/to/python/email/program.py
Python program:
1 #!/usr/bin/python
2
3 import imaplib
4 import os
5 import sys
6 import pynotify
7
8 if not pynotify.init('Emails'):
9 sys.exit(1)
10
11 with open('/path/to/python/email/count.tmp', 'r') as file:
12 data = int(file.read().strip())
13 client = imaplib.IMAP4_SSL('mail.sever.com', '993')
14 client.login('user#server.com', 'password')
15 client.select()
16
17 unseen = client.search(None, 'UnSeen')[1][0].split()
18
19 if unseen[0] == '':
20 pass
21 else:
22 if len(unseen) != data:
23 n = pynotify.Notification(str(len(unseen) - data) + " New Messages",
24 str(len(unseen)) + " Unread Messages",
25 "file:///path/to/python/email/mail.png")
26 if not n.show():
27 print "Fail"
28 sys.exit(1)
30 with open('/path/to/python/email/count.tmp', 'w') as file:
31 file.write(str(len(unseen)))
The script works correctly when run by itself, but it will not run when scheduled as a cron job. I checked the syslog and it says the script is being run, and I've run as sudo the line from the log to verify.
I've checked
Execute python Script on Crontab
and
Cron with notify-send
but neither, nor further links seem to address this combination.
Any thoughts?
-Update 1-
As pynotify seems not to allow the program to work at all, I've replaced their calls with an os.system call. At least this updates the small tmp file, but still no notify.
os.system('/usr/bin/X11/notify-send "{} New Messages" "{} Unread Messages"'.format(len(unseen) - data, len(unseen))
-Update 2-
/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py:57: GtkWarning: could not open display
warnings.warn(str(e), _gtk.Warning)
** (other.py:16509): WARNING **: Command line `dbus-launch --autolaunch=91bdcc4a583bfb56734fbe1200000010 --binary-syntax --close-stderr' exited with non-zero exit status 1: Autolaunch error: X11 initialization failed.\n
When you run your program manually, it works because the variable DISPLAY is being set to your current display (:0 in most cases), however when running the script via cron, there is no such variable being set and notifications doesn't work.
As mentionned in this answer, you should export both the display and the Xauthority, and finally running the script as your user (and not as root).
Try something like this :
* * * * * export DISPLAY=:0.0 && export XAUTHORITY=/home/<username>/.Xauthority && sudo -u <username> /usr/bin/python /path/to/python/email/program.py
(change <username> with your username)
Change your cron entry to
* * * * * /usr/bin/python /path/to/python/email/program.py >> /var/log/program.log 2>&1
and see what you get in log file program.log
You can add print statements to your program to debug better (as you will get them in program.log)
Try this command:
*\1 * * * * sudo -u user DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send 'Hi.'
In an answer to another question I described a generalised notify-desktop github gist written for use in cron and timer jobs. The script deals with identifying the desktop user(s) and offers a solution for Wayland as well as X11.
Under Ubuntu 20.04, the following Python script stored under /home/<user>/notify.py:
import subprocess
subprocess.Popen(['notify-send', 'Running. Finally.'])
sucessfully sends a message from my user's crontab (crontab -e, not root) with the entry:
*/1 * * * * export DISPLAY=:0.0 && export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus && /usr/bin/python3 /home/<user>/notify.py
For me, the DBUS_SESSION_BUS_ADDRESS parameter made the difference. Solutions without it didn't work.
I am really puzzled on this issue. I am using python's SimpleXMLRPC to provide services to a web application.
The problem is that when I start my xmlrpc server from the command line everything runs smoothly but when it is started through crontab it doesn't.
I have tried to hold the start-up via sleep and checking /sys/class/net/eth0/device/net/eth0/operstate but got no luck.
Please find attached the source for the script:
#!/usr/local/bin/python2.5
# -*- coding: utf-8 -*-
# License: GNU
# startxmlrpc.py: startup script for xmlrpc server to deal with processing
## {{{ http://code.activestate.com/recipes/439094/ (r1)
import socket
import fcntl
import struct
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
## end of http://code.activestate.com/recipes/439094/ }}}
import xmlrpclib
import urllib2
import os
from SimpleXMLRPCServer import SimpleXMLRPCServer
from time import sleep
def send(img1,img2,lib,filters):
global HOST_IP
path = '/var/www/%s/' % MD5Cypher(HOST_IP)
makedirs(path)
print "Path: %s" % path
if lib=='devel':
os.system("""python ~/devel_funcs.py %s %s "%s" &""" % (img1_path,img2_path, filters))
if lib=='milena':
import milena_funcs
milena_funcs.mln_process(img1_path, filters)
return HOST_IP + '/' + path.split('/var/www/')[1] + 'out.pgm'
while open('/sys/class/net/eth0/operstate').read().strip() != 'up':
sleep(5)
HOST_IP = get_ip_address('eth0')
server = SimpleXMLRPCServer((HOST_IP, 7070))
server.register_function(send)
server.serve_forever()
This is the error I get if I try to launch my process just after a clean boot:
<class 'xmlrpclib.Fault'>: <Fault 1: "<class 'xmlrpclib.ProtocolError'>:<ProtocolError for 192.168.0.5:7070/RPC2: -1 >">
args = ()
faultCode = 1
faultString = "<class 'xmlrpclib.ProtocolError'>:<ProtocolError for 192.168.0.5:7070/RPC2: -1 >"
message = ''
If I kill it and run it again, it works.
This is the crontab:
usrmln#Slave1:~$ crontab -l
# m h dom mon dow command
* * * * * python ~/master_register.py > /dev/null 2>&1
* * * * * python ~/startxmlrpc.py > /dev/null 2>&1
0 5 * * * find /var/www/ -type d -mtime +3 -exec rm -rf {} \; > /dev/null 2>&1
You don't show what the error is, but, it is possibly that PYTHONPATH is not being set when run from cron. You could set it before running the script.
Or, of course, you are running it as a different user and file permissions are not correctly set. Also ~/devel_funcs.py will not refer to your home directory if cron runs your script as a different user.
I finally got it, I was using python 2.5 locally and I had to add to the execution like this:
/usr/local/bin/python2.5 /home/username/startxmlrpc.py > /dev/null 2>&1