Call python command from dialplan

I have the following in a dialplan:

extensions_custom.conf:exten => 287,1,Answer()
extensions_custom.conf:exten => 287,n,Playback(this)
extensions_custom.conf:exten => 287,n,Playback(call)
extensions_custom.conf:exten => 287,n,Playback(is-now-being-recorded)
extensions_custom.conf:exten => 287,n,Record(/tmp/record287_%d.gsm)
extensions_custom.conf:exten => 287,n,System(/usr/local/bin/mailgsm ${RECORDED_FILE})

The mailgsm file works fine from the command line but is apparently not being executed by asterisk. The permissions on mailgsm are set to owner and group=asterisk. I know there are similar posts about running python within a dialplan but as yet I haven’t found a solution.

Which permissions are set for that user and group?

What is the first line of the script?

You can try running asterisk in the foreground with debug mode to see the returned error

@david551 the 1st line of the script is #!/usr/bin/python3. As I said, it runs fine from the command line.

ls -l /usr/local/bin/mailgsm
-rwxr-xr-x. 1 asterisk asterisk 282 Dec 19 11:36 /usr/local/bin/mailgsm

and if i 1st do su - asterisk fromthe command line the mailgsm command also works fine. Running asterisk in foreground is a little more complicated since asterisk is running in an lxc container and I’d prefer not to bring it down.

The console output of executing Asterisk in the foreground with debug and verbose cranked up may provide clues.

As suggested above, the ‘hash bang’ line of your Python script may also provide clues.

Comparing the PATH environment variable between the 2 execution environments (Asterisk system() and shell command line) may also provide clues.

Are you sure you normally execute Asterisk as the asterisk user and not root?

Since you’re running in a container, please be sure to execute suggestions in the container.

lxc is new to me, but a quick skim of its promotional documentation and wikipedia page makes me think this could well be lxc doing its job of isolating the container from the rest of the system.

@david551 I don’t think so, python and the command are all in the container. I am ssh into the asterisk machine (not the host). I think s omething else is preventing python from being called.

@sedwards Yes, Asterisk is running as user asterisk (not root).

What does one have to do after changing the extension_custom_conf file and the dialplan to have it take effect?

FreePBX? If so, maybe your questions will find more relevant experience on a FPBX forum.

I connected to asterisk with asterisk -r and reloaded the dialplan. Also turned debug and verbose to 10. Did tail of /var/log/asterisk/full and retried extn 287 and learned nothing from the log.

If you are not running FreePBX, only extensions.conf will be read unless something in that includes another file. Surely the FreePBX file should be extensions_custom.conf. (Note the “s” as well as the “;”.)

As you can see from the original post, the filename is correct and the dialplan for 287 works, except for the fact that the call to python is not being made. However, I just tried to change the earlier line which is the voice prompt and after changing it , I don’t hear the new prompt which makes me wonder where it is getting the dialplan from or if something more needs to be done for it to take effect.

Correction: a reload dialplan does enable changes. No python execution though.

Can you capture anything useful with the following:

exten => 287,n,System(/usr/local/bin/mailgsm ${RECORDED_FILE} >/tmp/execution-log 2>&1)

What happens if you copy python3 to /usr/sbin/ set the proper permissions, and update your script to #!/usr/sbin/python3

Does it work?

Also, not sure if you are aware, you can play multiple files in a single line.

exten => 287,n,Playback(your&call&is-now-being-recorded)

@sedwards It runs as before with nothing in execution.log.

@PitzKey I did as you suggested and the result is the same. File is recorded but the python script is not being executed.

I changed the line to:

exten => 287,n,System(/usr/bin/echo blah>/tmp/test)

Nothing is written to /tmp/test.