System() Command Refuses To Execute Bash Script

Home » Asterisk Users » System() Command Refuses To Execute Bash Script
Asterisk Users 8 Comments

Hi All

I’m using this extension to try and get Asterisk to run a bash script:

exten=>802,n,System(/bin/sh -f /root/

This file is

-rwxr-xr-x 1 root root 171 Mar 2 16:23

e.g. root owns the file, and it has execute permissions for all users.

Asterisk runs as root as well.

Asterisk executes the command without any errors at max verbosity.

The file contains:

#!/bin/bash touch wireless-executed

In my /root folder however, the file


is never written – indicating the script does not execute, even though it is owner by the same user asterisk runs as, and is world-executable.

How can I use System to run a bash script?




8 thoughts on - System() Command Refuses To Execute Bash Script

  • wrote on 03/02/2015 08:27:07 AM:


    Just to rule out some weird permissions issue, try to write the file to some directory that has full read/write permissions to everyone (eg 777). If the file can be written to that directory you probably have a permissions issue still. I run my asterisk under the asterisk user and have it kick of scripts that write to a folder on the system all the time. The folder has full permissions for the Asterisk user.

    Give it a shot and see what you get.

  • Hi all

    I got this solved.

    Turns out the script WAS executing, but I forgot that apparently you need to follow “cron rules” in any BASH scripts executed via System() from an Asterisk dialplan.

    E. g. all paths must be fully and absolutely specified, there are no relative path references available.

    So I changed the the file which was:

    #!/bin/bash touch wireless-executed


    #!/bin/bash touch /root/wireless-executed

    and now if I phone 800 on my Asterisk context I can see that the file


    is created by the touch binary, which indicates that Asterisk has run the script successfully.

    So the problem was not Asterisk or BASH or permissions, but rather that it appears that all paths in any System() script must be absolutely, not relatively, specified.

    Hope this helps somebody – simply follow “cron rules” when writing Asterisk-callable BASH scripts which you plan to trigger via the System()
    Asterisk dialplan application.



  • Hi,

    Some notes,

    Don’t run Asterisk as root.

    But also:

    A variant on:

    cd `dirname $0`

    can help in such cases.

  • Not quite.

    The ‘base’ for relative paths would be the ‘cwd’ (current working directory) of the Asterisk process.

    You can show the cwd for your running Asterisk by:

    sudo ls -l /proc/$(pidof asterisk)/cwd

    which is a link to the process’s cwd.

    I suspect if you search your file system (‘sudo find / -name wireless-executed’), you will find ‘wireless-executed’ — probably in the directory shown by the above command.

    You can set this in the script that starts Asterisk. I set mine to /tmp/
    (‘cd /tmp/’) so I know where any random file access will occur, relatively speaking.

  • This would yield ‘the directory the script lives in’ which may be read-only and is probably not where you want random files created.

  • I’m surprised that you didn’t have to specify the full path to the ‘touch’
    command. When writing AGI scripts, I always do something like
    $touch = which( ‘touch’ ). I guess it’s over kill. John

    —–Original Message—

  • You should generally not need a path to commands in /bin / /usr/bin .

    If ‘which touch’ returned it, it’s in the path.

  • Please don’ top post.

    The AGI process inherits the environment of the parent, Asterisk.

    You can set the Asterisk environment in the script that starts Asterisk.

    For example:

    cd /tmp/
    ulimit -n 8192
    nice –adjustment=-20\
    env –ignore-environment\