I have a periodic script running at background in my Ubuntu Server. If I execute it in RStudio, everything works as expected. But when I execute via terminal with Rscript, it gets stuck when calling a python script (it isn't always... but many times it gets stuck there). (I know it gets stuck there because when I stop the Rscript, it always tells me it was running my python script). I have chmod 777
'ed to that python script but no clue.
This worked normally until some days ago, doesn't know why.
The Rscript sentence:
Rscript /home/XXX/XXX/scriptServicioBBDDHS.R
The place where it stops in the R code:
outputMACs <- system(ignore.stdout = F, ignore.stderr = T,
"python3 /home/XXX/XXX/MACsPrincipal.py 'Y.Y.Y.Y' 'user' 'password'", intern = T)
The python script is an API for a MikroTik router. It gets stuck when trying to read the response from the router. In this sentence:
r = select.select([s, sys.stdin], [], [], None)
I put you the code in the main()
of the python script:
def main():
s = None
for res in socket.getaddrinfo(sys.argv[1], "8728", socket.AF_UNSPEC, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
s = socket.socket(af, socktype, proto)
except (socket.error, msg):
s = None
continue
try:
s.connect(sa)
except (socket.error, msg):
s.close()
s = None
continue
break
if s is None:
print ('could not open socket')
sys.exit(1)
apiros = ApiRos(s);
apiros.login(sys.argv[2], sys.argv[3]);
inputsentence = ['/ip/hotspot/active/print', '=detail=']
apiros.writeSentence(inputsentence)
t_end = time.time() + 2
while time.time() < t_end:
r = select.select([s, sys.stdin], [], [], None)
if s in r[0]:
# something to read in socket, read sentence
x = apiros.readSentence()
Thank you for your help. This script used to always work when I first used it in the beggining, using the crontab. Now it just fails.
Sergio.
I found a possible bug in the MikroTik API. The problem was in the sentence
r = select.select([s, sys.stdin], [], [], None)
I had to rewrite it to omit the sys.stdin
r = select.select(s, [], [], 0.0)
And for that to work I have to do a system call via R with the following arguments:
system(ignore.stdout = F, ignore.stderr = T, "python3 /home/XXX/XXX/MACsPrincipal.py 'Y.Y.Y.Y' 'user' 'password'", intern = T)
It doesn't work if you don't ignore the stderr
output.