Python on Crontab does not execute bash script - python

import subprocess as sub
import re
import os
from datetime import datetime as influx_timestap
from influxdb import InfluxDBClient
from collections import OrderedDict
insert_json = []
hostname = str(sub.check_output('hostname')).strip()
location = str(sub.check_output(['ps -ef | grep mgr'], shell=True)).split()
current_dir = os.getcwd()
print("script executed")
gg_location_pattern = re.compile(r'mgr\.prm$')
gg_process_pattertn = re.compile(r'^REPLICAT|^EXTRACT')
for index in location:
if gg_location_pattern.search(index) != None:
gg_location = index[:-14]
os.chdir(gg_location)
print("checkpoint1")
get_lag = sub.check_output(str(current_dir) + '/ggsci_test.sh', shell=True)
print("checkpoint2")
processes = get_lag.split("\n")
for process in processes:
if gg_process_pattertn.search(process) != None:
lag_at_chkpnt = int((process.split()[3]).split(":")[0]) * 3600 + int((process.split()[3]).split(":")[1]) *60 + int((process.split()[3]).split(":")[2])
time_since_chkpnt = int((process.split()[4]).split(":")[0]) * 3600 + int((process.split()[4]).split(":")[1]) *60 + int((process.split()[4]).split(":")[2]
)
process_dict = OrderedDict({"measurement": "GoldenGate_Mon_" + str(hostname) + "_Graph",
"tags": {"hostname": hostname, "process_name": process.split()[2]},
"time": influx_timestap.now().isoformat('T'),
"fields": {"process_type": process.split()[0], "process_status": process.split()[1],
"lag_at_chkpnt": lag_at_chkpnt, "time_since_chkpnt": time_since_chkpnt}})
insert_json.append(process_dict)
host = 'xxxxxxxx'
port = 'x'
user = 'x'
password = 'x'
dbname = 'x'
print("before client")
client = InfluxDBClient(host, port, user, password, dbname)
client.write_points(insert_json)
print("after client")
This code works manually perfect, but on the crontab it is not working. After searching on the internet I found that they say change or set your "PATH" variable on the crontab. I changed my "PATH" variable and it is still not working.
Crontab log file write "checkpoint1" after that there is nothing. So, line not working is "get_lag = sub.check_output(str(current_dir) + '/ggsci_test.sh', shell=True)"
What can I do here afterwards?
Take care,

it looks like your external script (ggsci_test.sh) has some issues with the paths / general failure.
From the Python subprocess documentation about subprocess.check_output:
If the return code was non-zero it raises a CalledProcessError. The
CalledProcessError object will have the return code in the returncode
attribute and any output in the output attribute.
So thats the reason why you see the error when catching it, but not being able to continue.
You should check therefore if your shell script has any issues that need to be solved before.

Related

Knowing if my server is up or down using ping. .

Thanks to all for your time.
I'm trying to know if several server are up or down using ping, and it works . . . but when I try convert the result into a up or down, something is wrong and always is down.
Dunno what other thing I should try, don't need anything else, just up or down and the IP.
import os
import datetime
import platform
import subprocess
import string
date = datetime.datetime.now()
day = date.day
hour = date.hour
def writedoc ():
os.chdir ('Path')
wresult = open ("pingresults_{}_{}.txt".format(day,hour), 'a')
wresult.write ('{}-{}\n'.format(ips, rping))
wresult.close ()
os.chdir ('Path')
openips = open ("ips.txt","r")
ipfile = openips.readlines()
for ips in ipfile:
ips = ips.strip()
print (ips)
args = ["ping", "-n", "4", "-l", "1", "-w", "1000", ips]
pping = subprocess.Popen(args, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
rping = pping.stdout
for line in rping:
print (line)
if (rping.find("(100% perdidos)" != -1)):
result = "down"
print (result)
else:
result = "up"
print (result)
writedoc()
if (rping.find("(100% perdidos)" != -1))
Should this instead be
if (rping.find("(100% perdidos)") != -1)
So that this checks that rping.find("(100% perdidos)") does not return - 1.
With your example you are effectively calling rping.find(True) as
"(100% perdidos)" does not equal - 1.

Crontab doesn't updates variable in .txt file

I've got problem with my python script.
#!/usr/bin/python
import sys
import MySQLdb
import os
import time
import datetime
import glob
jaki = 0
plik = open('aktualny.txt')
otwarcie = open('Pomiary.txt')
try:
pomiar = plik.read()
czest = otwarcie.read()
except:
print ("nie ma takiego pliku")
finally:
czestotliwosc = int(czest)
ile = int(pomiar)
if ile == czestotliwosc-1:
ile = 0
plik = open('aktualny.txt','w')
pomiar = str(ile)
plik.write(pomiar + '\n')
plik.close()
otwarcie.close()
jaki = ile
else:
ile = ile + 1
plik = open('aktualny.txt','w')
pomiar = str(ile)
plik.write(pomiar + '\n')
plik.close()
otwarcie.close()
jaki = ile
plik = open('aktualny.txt','w')
plik.write(pomiar + '\n')
plik.close
If I run this script from terminal, everything is OK (that means in "aktualny.txt" is current value), but if I use crontab to do this script every 1 minute, "aktualny.txt" is blank, there is no variable. I give all permisions to all files and scripts, but it doesnn't solve the problem.
In "Pomiary.txt" I've got constant "2".
What is wrong with it?
Followed Martijn Pieters's post: "You are using relative paths for the files you open. The current directory when running a crontab will differ from when you run it directly. Use absolute paths for the files and you'll see the script is working fine."

One python script executes, another does not, when using cron

I have a python script, followback.py, which I am trying to run by using cron.
The script runs fine on its own i.e. when run by command 'python followback.py'.
But the script is never run when using cron.
My crontab file:
* * * * * python /home/ubuntu/./followback.py
* * * * * python /home/ubuntu/./test.py
I am using test.py as a simple testing measure by writing to a file to let me know that it have been run.
followback.py:
import io, json
def save_json(filename, data):
with io.open('{0}.json'.format(filename),
'w', encoding='utf-8') as f:
f.write(unicode(json.dumps(data, ensure_ascii=False)))
def load_json(filename):
with io.open('{0}.json'.format(filename),
encoding='utf-8') as f:
return f.read()
CONSUMER_KEY = xx
CONSUMER_SECRET = xx
OAUTH_TOKEN = xx
OAUTH_TOKEN_SECRET = xx
auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
CONSUMER_KEY, CONSUMER_SECRET)
twitter_api = twitter.Twitter(auth=auth)
q = 'followback'
count = 20
page = 1
results = []
maxResults = 50
filename = 'attempted_accounts'
try:
usedUsers = json.loads(load_json(filename))
except IOError:
usedUsers = []
usedList = [used['id'] for used in usedUsers]
# search for 'followback' and follow the ones with 'followback' in description
while len(results) < maxResults:
users = twitter_api.users.search(q=q, count=count, page=page)
results += [user for user in users if 'followback' in user['description'] and user['id'] not in usedList]
page += 1
[twitter_api.friendships.create(user_id=user['id'], follow='true') for user in results]
out = usedUsers + [{'id' : e['id']} for e in results]
save_json(filename, out)
The script above simply searches twitter for users with followback in the description and follows them.
The test.py script runs fine through cron but followback.py does not and I have no clue as to what could be wrong.
Any suggestions?
Check if the followback.py file ex executable, if not use chmod +x and it should work. That's the most common issue with this. Look at similar case here.

Python IRC bot system uptime

I'm trying to show system uptime in my irc bot. The script I'm using is:
#linux
import os, sys
from datetime import timedelta
from util import hook
import subprocess
import datetime
#hook.command
def uptime_linux(inp,say=None):
with open('/proc/uptime', 'r') as f:
uptime_seconds = float(f.readline().split()[0])
uptime_string = str(timedelta(seconds = uptime_seconds))
say(uptime_string)
# windows
def uptime():
"""Returns a datetime.timedelta instance representing the uptime in a Windows 2000/NT/XP machine"""
if not sys.platform.startswith('win'):
raise RuntimeError, "This function is to be used in windows only"
cmd = "net statistics server"
p = subprocess.Popen(cmd, shell=True,
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
(child_stdin, child_stdout) = (p.stdin, p.stdout)
lines = child_stdout.readlines()
child_stdin.close()
child_stdout.close()
lines = [line.strip() for line in lines if line.strip()]
date, time, ampm = lines[1].split()[2:5]
#print date, time, ampm
m, d, y = [int(v) for v in date.split('/')]
H, M = [int(v) for v in time.split(':')]
if ampm.lower() == 'pm':
H += 12
now = datetime.datetime.now()
then = datetime.datetime(y, m, d, H, M)
diff = now - then
return diff
#hook.command
def uptime_win(inp,say=None):
if __name__ == '__main__':
say(uptime())
It doesn't give me an error, but it doesn't show. I've looked at the code, I don't see why I'm not able to see it.Maybe it might something small but I don't see it :D. I have the needed modules included, and it still doesn't work :'(. Also I'd want to ask if any of you have easier method to get uptime for windows (I have for linux already).Thanks!
I don't see what's wrong right now, but in case it helps a bot I worked on did something similar, maybe you can take a look there:
uptime() at https://bazaar.launchpad.net/~p1tr-dev/p1tr/main/view/head:/plugins/info.py
using _get_output defined at https://bazaar.launchpad.net/~p1tr-dev/p1tr/main/view/head:/lib/plugin.py
I think that you are not in the main module so you have to remove if __name__ == '__main__':
Haven't tested on Windows as I don't have a Windows box handy, but using psutil (which is supposed to be cross platform)
>>> pid = psutil.Process(1) # get main process (kernel or close to it)
>>> pid
<psutil.Process(pid=1, name='init') at 31222480>
>>> pid.create_time # create_time is effectively system up time (or should be close to it)
1356597946.03

Get Windows Process/File Description in Python

I have the following code so far that tells me every time a new process is created.
import wmi
c = wmi.WMI()
process_watcher = c.Win32_Process.watch_for("creation")
while True:
new_process = process_watcher()
print(new_process.Caption)
print(new_process.ExecutablePath)
This works fine, but what I'm really trying to do is get at the Processes Description because while the filename of what I'm looking for might change, the description does not. I can't find anything in Win32_Process or win32file that gets me the file description though. Does anybody know how to do this?
Thanks!
while True:
try:
new_process = process_watcher()
proc_owner = new_process.GetOwner()
proc_owner = "%s\\%s" % (proc_owner[0],proc_owner[2])
create_date = new_process.CreationDate
executable = new_process.ExecutablePath
cmdline = new_process.CommandLine
pid = new_process.ProcessId
parent_pid = new_process.parentProcessId
privileges = "N/A"
process_log_message = "%s,%s,%s,%s,%s,%s,%s,\r\n" % (create_date,proc_owner,executable,cmdline,pid,parent_pid,privileges)
print "1"
print process_log_message
log_to_file(process_log_message)
except:
print "2"
pass
Hope this helps :)

Categories

Resources