The script is not executed when I run asterisk from systemctl

Hi everyone!

When I run asterisk from systemctl, scripts using applications are not executed for example:

Description=Asterisk PBX and telephony daemon.
#include these if asterisk need to bind to a specific IP (other than

#if systemd do not provide hostname and you need to use ${ENV(HOSTNAME)}
ExecStart=/usr/sbin/asterisk -mqfg -C /etc/asterisk/asterisk.conf
ExecReload=/usr/sbin/asterisk -rx 'core reload'
#if /var/run is a tmpfs, this will create /var/run/asterisk on start


# Prevent duplication of logs with color codes to /var/log/messages


exten => 111,n,System(${SEND_CALL})


exten => 111,n,MixMonitor(${INRECPATH}${RECFILENAME}.wav,,/var/lib/asterisk/scripts/

it runs from asterisk

asterisk 326055 5.4 1.9 1200664 77744 ? Ssl 12:17 1:17 /usr/sbin/asterisk -mqfg -C /etc/asterisk/asterisk.conf


astcachedir => /tmp
astetcdir => /etc/asterisk
astmoddir => /usr/lib/asterisk/modules
astvarlibdir => /var/lib/asterisk
astdbdir => /var/lib/asterisk
astkeydir => /var/lib/asterisk
astdatadir => /var/lib/asterisk
astagidir => /var/lib/asterisk/agi-bin
astspooldir => /var/spool/asterisk
astrundir => /var/run/asterisk
astlogdir => /var/log/asterisk
astsbindir => /usr/sbin

but in case you run it bash without changing dialplan like this:

root@asterisk:/home/user# su - asterisk
asterisk -vvvgc

it works as expected


-rwxrwxrwx 1 asterisk asterisk 635 Jul 15 23:43 /var/lib/asterisk/scripts/

This permission is very bad practice. You should give it no more than rwxr_xr_x, and it is best practice for non-set user scripts to be owned by root, with group root, so that they cannot be changed by a compromised application running on the working account.

As to the original problem, I suspect you are using incomplete file names and sysctl isn’t setting up a complete environment, in particular PATH.

Thanks for the answer

Changed privileges

-rwxr-xr-x 1 root root 647 Jul 16 16:18

Can you please explain in more detail about the names?

If you have a #! line which doesn’t have a full path name, you will need the correct PATH variable. Similarly for other file names that are looked up in a list of directories from the environment.

Also, had did you find out the PATH in Asterisk environment?

What do you get from the shell command:

cat /proc/<insert the PID of the main asterisk process here>/environ

I don’t have asterisk on this machine, so I’ll use BOINC as a proxy:

root@dhcppc4:~# ps -e | grep boinc
  712 ?        00:03:45 boinc
root@dhcppc4:~# cat /proc/712/environ

(There are NULs in this output.)

Thanks for reply!

Script shebang

#!/usr/bin/env python3
cat /proc/ASTERISK_PID/environ


When I enable full debug no any errors just message that script executed

Does it make any assumptions about standard input/output/error?

(In any case, you should probably redirect standard error to a file, for debugging.)

Did you mean script ?

“it” refers to your script.

This is a simple script there can be no errors. I mean no dynamic variables

But you can still get errors from the shell, from /usr/bin/env, or from the python interpreter.

I made a simple script, specifically making an error to check whether the error will be written to the file

wwrite is mistake

#!/usr/bin/env python3

logf = open('/var/lib/asterisk/my.log', 'w+')

    with open('/tmp/simple.txt', 'w+') as f:
        f.wwrite('EXEC FROM ASTER')
except Exception as e:

asterisk output CLI

  == MixMonitor close filestream (mixed)
  == Executing [/var/lib/asterisk/scripts/]
  == End MixMonitor Recording SIP/protei-000028fc

If you execute this file on behalf of asterisk in bash
, the file is created, but the application (MixMonitor or System) not.

That’s basically not surprising, given what you’ve already said, and the failure is probably happening before any user code is executed, so the log file can’t be used.

How do you think it is better to catch the error?

As previously noted.

  == Executing [/var/lib/asterisk/scripts/ 2> /tmp/err.log]

/tmp/err.log not created

finally found the problem:

root@asterisk:/var/lib/asterisk# cat my.log 
[Errno 2] No such file or directory: '/tmp/t.wav'

root@asterisk:/var/lib/asterisk# ls /tmp/t.wav 

it can not find file @_@ which exist

The problem was always there, I changed the rights to the audio recording file and everything worked.

I know that is basic but if someone will have problem using python script
use try, expect

except Exception as e:
    logf = open('/var/lib/asterisk/my.log', 'w+')

then read my.log file


Thanks you @david551

You pointed in the right direction.

Using syslog is preferable to private log files for several reasons.

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