Linux – Command not executing inside shell script

linuxshell-scriptUbuntu

#!/bin/bash
value=$(</var/www/sym_monitor/man.txt)


if [ "$value" == "true" ]; then

     ps -ef|grep sym |grep -v grep |awk '{ print $2 }'|sudo  xargs kill -9;


(cd /var/www/symmetric-ds-3.1.6/bin;sudo ./sym --port 8082 --server);

fi

The second command inside brackets is not executing any idea why this is happening?

Best Answer

You must use an absolute path with sudo, for security reasons:

( sudo /var/www/symmetric-ds-3.1.6/bin/sym --port 8082 --server );

Check the output of sudo -l to confirm. From the sudoers man page (1.7.x):

A Cmnd_List is a list of one or more commandnames, directories, and other aliases. A commandname is a fully qualified filename which may include shell-style wildcards (see the Wildcards section below).

sudo xargs works because xargs is (almost certainly) found in a trusted path (/usr/bin).

Also, check out pgrep and pkill, it will save you the needless ps pipe acrobatics.

You have the potential for resource leaks and other unwanted behaviour with an unconditional kill -9, see https://unix.stackexchange.com/questions/8916/why-not-kill-9-a-process .

Update you've added that you run this via root's crontab -- root has no need to use sudo, and in some cases root may be prevented from running sudo, check what sudo -l says when you are root. If you want to to be able to start a program (that doesn't switch its own uid) as a specific userid then the common way is su - username -c "command" .

Related Question