Action relay on call number


It’s my first time working with asterisk and I came across the following project.

I’m working with raspberry pi with installed RasPBX.

The purpose of the project is to action a relay when an internal number receives a call.

I create a python script that action the relay (tested and it works if it is launched manually via cli).
The problem is that I can’t understand to trigger a python script when the internal number (300) receives a call.
I tried to add a command into extensions_custom.conf file like:

exten => 300,1,System(python /path_file/


exten => 300,1,AGI(/path_file/

and other similar attempts…

I don’t understand if it does not work because there is an error in the code or some other steps and configuration which I didn’t do.

Thank you

Specifying the full path to the python binary usually does the trick for me. You could also include an appropriate #! line at the top of the python file, pointing to the python binary you want to run.

The path of your console session, and the path when running the System application, can be very different.

Also, while for this project it may not matter much, be aware of the security implications if using caller controlled/influenced variables with calls to System.

Additionally just adding an “action_relay” context doesn’t mean it’ll be used. If FreePBX is in use, you also need to find out how to get your context used.

Thanks to both of you for the suggestions.

I add the #! string into python file and i delete the context.
I used AGI command.

Now when I call the internal number into log file i see this two lines:
res_agi.c: Launched AGI Script /path_to_file/
res_agi.c: <SIP/xxxxxxx>AGI Script completed, returning 0

I presume that the python script was running but the relay remained off.

Am I still missing something?

Lots of little things can get in the way of your success.

Something as simple as ‘I wrote this on my Windows box and FTP’d it over without translating line endings’ can be really hard to figure out.

Adding a simple line like:

system("touch /tmp/yay")

near the top of your script may give you a clue that your script is actually executing or not.

Running your script in a shell is a different environment than as the user executing Asterisk.

For example, running your script in a shell like:

sudo --user=<user-executing-asterisk> env --ignore-environment /path_to_file/

may yield some clues.

Permissions are also a frequent culprit. You can change permissions to ‘777’ (a true mark of a newbie) as long as you remember to change them back. If you have a permissions problem, seek to grant as little (only read and/or execute) access as possible.

Maybe moving your script to an Asterisk directory (like astagidir which is usually /var/lib/asterisk/agi-bin/). Your script isn’t an AGI, but ‘agi adjacent.’ :slight_smile:

Thank you for the answer.

I tried your suggestion, the script run correctly if start into shell but not run when i call the number.

I think the problem might be my procedure for creating extensions or dialplans.
I’ll try to recreate from scratch.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.