Page 1 of 1

Issue with AOS command called from cronjob

Posted: 22 Nov 2022 10:43
by HStr
Hello,

I want to call AOS commands at an intervall. That's why I've created a python script and added it to a cronjob. The script is called by cronjob as expected (checked by updated content of cron.log). But the execution of AOS command (os.popen(cmd).read()) is not working, an exception is not thrown. However, if I call the python script manually from CLI the AOS command is executed as expected.

The python script /flash/python/cron.py:
#!/usr/bin/env python3

import os,datetime

def exec_cmd(cmd):
  try:
    cmd_res=os.popen(cmd).read()
    return cmd_res.strip()
  except:
    return ''

# testlog when the script was executed
f=open('/flash/cron.log','w')
f.write(datetime.datetime.now().strftime('%d.%B %Y   %H:%M:%S')+'\n')
f.close()

# main
exec_cmd('interfaces 8/1/9 admin-state disable')

BTW, the file permissions of the script are set to 755.

Regards Holger

Re: Issue with AOS command called from cronjob

Posted: 23 Nov 2022 11:23
by silvio
Hi,
there are some hints in the switch management guides.
For executing a command there is written f.e.: os.system("show microcode") instead of exec_cmd. Maybe this is working too.

For using a cron-job to start a script you need to use the AMS.

Code: Select all

> appmgr start ams cron-app
> write memory flash-synchro
f.e.:

Code: Select all

> appmgr start ams cron-app argument "30 python3 /flash/python/XX.py , 60 python3 /flash/python/YY.py”
Every 30 minutes the script XX.py will be executed and every 60 min the other.

To stop and restart use:

Code: Select all

> appmgr stop ams cron-app
> appmgr restart ams cron-app
I hope that will be helpfull for you.
BR Silvio

Re: Issue with AOS command called from cronjob

Posted: 24 Nov 2022 01:48
by HStr
Hi Silvio,

Thank you for your suggestions. I've already tried all of that. ALE support suggested the same. But the cronjob itself is not the issue. The script will be executed at the desired interval. All python commands except the system call (e.g. interfaces 8/1/9 admin-state disable) are executed. When I execute the python script manually from CLI the system call works as expected.

Regards Holger

Re: Issue with AOS command called from cronjob

Posted: 24 Nov 2022 06:13
by silvio
So I assume you know this link too: https://dokuwiki.alu4u.com/doku.php?id= ... on-cronjob

I think you need to force the support and present us the solution (if the support has anyone sometimes).
BR Silvio

Re: Issue with AOS command called from cronjob

Posted: 24 Nov 2022 09:54
by HStr
Hi Silvio,

Yes I know this link. I try to force the support, although they probably don't understand the issue. As soon as there is a solution I will present it here.

Regards Holger

Re: Issue with AOS command called from cronjob

Posted: 01 Dec 2022 08:43
by HStr
Hi,

As promised I provide the solution from ALE support:
After some investigation in lab, we figured that cron-app is executing the command in the "su" linux shell.
In order to disable the interface you can use this new script calling the "interfaces" module directly.
----------------------------------------------------
from subprocess import Popen, PIPE, STDOUT

process = Popen(['/vroot/bin/interfaces', 'port', '8/1/9','admin-state','disable'], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
----------------------------------------------------
I also tried that with other commands in /vroot/bin (e.g. lanpower) and it works fine. :-)
The reason for my request is to check the status/reachability of PoE clients continuously and try to restart crashed devices by deactivate the link or switch off and on affected PoE ports to restart these devices.

BR Holger

Re: Issue with AOS command called from cronjob

Posted: 01 Dec 2022 09:57
by silvio
many thanks for sharing the solution.
best regards
Silvio