Why 'Command not found' to run 'sudo' by shell_exec in PHP?


I'm trying to run command line using shell_exec in PHP as

$cmd_ret =shell_exec("/etc/a_script.sh start 2>&1");
echo $cmd_ret

The '$cmd_ret' saying that 'sudo: flock: command not found'
since the 'a_script.sh' has a code like this

nice sudo flock -x /var/run/temp.lock -c "/etc/another_script.sh start"

To resolve this, just remove the command word 'sudo' from above.

My question is that
why this happened?

Give me an insight gurus.

I dont think sudo will exist normally since by default openwrt only has the single logon (root)

What do you see when you run “which sudo”

Thanks for the reply.

  • Yes, I have installed 'sudo' before and using it without problem in other circumstances.
  • I can see that '/usr/bin/sudo' by the 'which sudo'.
    Is this should be a clue?

Is it actually flock that its complaining it can’t find?

sudo su -
which flock

You don't need to install sudo, you simply need to omit the command.

You're already running as Superuser (root).

It says

# sudo su -
sudo: su: command not found
# which flock

I know the default account is the 'root',
but my system has several non-root account like transmission as so.
So I need 'sudo' since the series of scripts are common/shared script across all other processes.

my question is that
why the symptom happened that not find the command by sudo?

Ok , so then logon as root then and see if it can find flock

From the command line, everything is working fine. But the shell_exec.
That is why I'm asking for...

This is very reminiscent of a recent problem where you were trying to run as a non-privileged user and apparently trying to write a lock file into a directory owned by root without write permission. As I recommended in that thread, you should probably create a run directory for your lock files that is owned by the process that is going to write the locks.

As an aside, PHP and elevated privileges is a very bad combination.

"Less aside", if sudo works without a password, from a security standpoint, you might as well be running as root.

1 Like

I'm not asking you that workaround or best practice
but the reason why. You keep miss pointing out from time to time.

  • As the previous thread, you didn't even suggested any workaround to me(I saw the reply that deleted which the rude and aggressive without any solution or advice but complaining).
  • I'm not looking for a one who teaching me since 30 years of IT guys in this IT industry who fully understand what is what.
  • And now again, I feel that you don't even understand the why the symptom happened.
  • You keep under estimate people's effort to investigate the root of the problem.
  • If you don't have any hint TO HELP people, just may keep silence. You make people uncomfortable and totally miss-point-out of the question. If you are not so good at English to give people your thinking, you may keep silence.

Jeff, you, hope that you please keep distance away from my post further.

  • Can you confirm that you successfully installed and used sudo on OpenWrt before?

What does which su say? Is it able to find the command su?

Yes. Successfully installed as

# which sudo

Regarding the su...
I haven't installed. So, system saying that

# which su
# su
-ash: su: not found

(Which mean that no output by the command 'which su').

You may try to execute the command sudo env, take a look on the environment variable PATH to see if it include the directory /usr/bin

If it doesn't, then it will not find the executable /usr/bin/flock.

1 Like