If your program needs to be started from a certain directory, maybe because it is reading some files from there with relative paths, just change the directory first:
cd /home/root/test && mybinary 1 2
(where mybinary
is looked up in $PATH
), or:
cd /home/root/test && ./mybinary 1 2
To run the mybinary
executable in that directory, or another relative or absolute path to that file such as /home/root/test/mybinary
.
You may add another cd
to change back after finishing:
if cd /home/root/test; then ./mybinary 1 2; cd -; fi
The latter is superfluous if running from crontab. A better general approach to have only one (or a set of) command run in a separate directory is to use a subshell:
(cd /home/root/test && ./mybinary 1 2)
The GNU, NetBSD and FreeBSD implementations of env
(but not those of toybox or busybox as more commonly found on embedded systems) also have a -C
option to change the working directory the command is run in:
env -C /home/root/test ./mybinary 1 2
Creating a separate script for this purpose seems over-engineering.
Don't forget a comment in the crontab. If you are the author of said program, you should read from fixed config file like ~/.mybinaryrc or ~/.mybinary.conf or the like, or provide a directory switch, where to look for the files, so you can call it like that:
mybinary --startdir ~/home/root/test 1 2
1 2
) are exactly the same as when you invoke it with your current working directory in the same folder where the program is installed. It's a matter of the code in the program itself having trouble with how you're invoking it, not the shell or the Linux kernel. Does the program's documentation discuss its requirements about where your working directory must be? Is this a program you have written?