1

This is a embedded Linux system so things are a little different. I am trying to run an binary with arguments using absolute path but it doesn't work. If I run the binary from the folder it works e.g. if the binary is in /home/root/test folder and I run

root@user:~/test# ./mybinary 1 2 

it works with no issue. But below will not work, seems it is not able to understand the arguments

root@user:~# /home/root/test/mybinary 1 2 

In the end I want to run this binary using crontab where I would use the absolute path to call it.

8
  • As you point out, this binary program is being invoked by the shell successfully, and the command-line arguments (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?CommentedFeb 21, 2023 at 18:21
  • @ilkkachu This is a embedded Linux system so things are a little different. Issue is, I cannot run this from anywhere except directory where it is present. If I run test/mybinary 1 2, it will not work. I believe it is something to do with how to run binaries with arguments from any path.
    – adi_226
    CommentedFeb 21, 2023 at 18:59
  • 1
    Please don't add information in the comments, @adi_226. The note about it being an embedded system was really important and could have got lost amongst comments rather than being edited into your question. Please do tell us what exactly you mean by "But below will not work, seems it is not able to understand the arguments". Error messages?CommentedFeb 21, 2023 at 19:10
  • @SottoVoce Thank you for your response. I believe that was the issue. So the binary tries to read information from files on the system. I didn't had those files mentioned as absolute paths in my code and there it was facing issue and giving the error. I changed those paths to absolute paths and it is working .
    – adi_226
    CommentedFeb 21, 2023 at 19:20
  • 1
    I’m voting to close this question because because it's now working; the OP pointed out that the "arguments" are filenames and that adding the full path to the arguments as well resolved the issue.
    – tink
    CommentedFeb 21, 2023 at 23:56

1 Answer 1

1

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 

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.