Reputation: 2888
I've got a shell script which I am trying to run as a specific user. My command looks like this:
su - jetty sh ./runProgram.sh
When I attempt to run this command through the console I get an error saying:
/bin/sh: /bin/sh: cannot execute binary file
I also tried:
su - jetty sh runProgram.sh
And I still get the same error..
It DOES work if I do this:
sh runProgram.sh
But this shell script is meant to be run by a specific user. Any advice on how to get this working??
Upvotes: 1
Views: 8567
Reputation: 1361
According to su
's documentation (info coreutils 'su invocation'
), it will by default execute a shell, and the arguments to su
are passed as arguments to the shell.
The implication is simply this: su
is doing in essence:
/bin/sh
*arguments_to_su*
but it does it as another user (the "effective user id")... that's all... So
su - jetty sh ./runprogram.sh
is akin to
(become the user jetty via login or su)
/bin/sh sh ./runprogram.sh
...and the shell will report an error, because the first /bin/sh
, called by su
, is trying to run the program sh
as a shell script, with ./runprogram.sh
as its argument. But sh itself is not a shell script, it is a binary (whose job it is is to run shell scripts).
if you were to simply do this:
su - jetty ./runprogram.sh
Then the su
command will call /bin/sh
with the program ./runprogram.sh
as its argument, and jetty as the effective user id, and all should be well. ...SHOULD be well, because since you are doing an su -
you are making the shell a login shell and changing to the user's home directory. If runprogram.sh
is not in the home directory, you will get an error.
This is why, also, you cannot run for example run a cp
command by simply:
su - jetty cp file1 file2
...because, again, after su
changes effective user id to jetty it will try this:
/bin/sh cp file1 file2
...and cp
is not a shell script. But the -c
option works in this case; because you are telling su
that you want to run /bin/sh
with the -c option to the shell:
su - jetty -c "cp file1 file2"
does the job. Note that you must quote the command, because the entire string is passed to the shell and (I believe) any following arguments are ignored.
Finally, the previous poster's answer doesn't work for me on Linux, it requires the entire command string to be quoted.
Upvotes: 2