I am developing a binary executable program command to execute programs and return the output into a text field.
The parameters to the command involve the command itself as it would be typed on the command line, and the directory. So the routine that executes the operation first switches to the directory, then executes the command.
For example if I want to execute the command some.cmd
in the directory /home/user
the parameters are command = 'some.cmd'
and directory = '/home/user'
.
What I have found is that some.cmd
does not work but if I change command to /home/user/some.cmd
the command works. However the command ls -l
works. I also notice that the cd
command is not recognized. If I run it remotely via ssh
such as setting command to ssh user@localhost 'cd /home/user && ./some.cmd'
it works.
It seems that some settings which are present when the command is executed in a shell are not present when it is run directly, but doing it via ssh
seems to create the settings for it work.
Is there some explanation for this?
UPDATE:
After some enquiries I got to learn that the API used for executing the commands were not being executed in the shell, or were not executed with the normal environment available from the console. After executing the commands with the /bin/sh -c "cd ..."
option the problem is no more. This is the environment doing ssh user@localhost 'command ...'
gave me.I am not so sure of the technical details, but apparently the existence of the environment available when you execute in your normal shell is not always available to commands executed directly by the OS.
cd
before running your command? Why not justssh user@localhost /home/user/some.cmd
? – roaima May 15 '15 at 14:53directory
parameter. – Mikel May 21 '15 at 13:32ssh user@localhost
so I started with that as a working assumption and asked whether you really needed thecd
for your application to work. Once I had the answer to that I would have been able to make a decision how to proceed with an offered solution. – roaima May 21 '15 at 13:58