How can I run a command that is in a different directory so that the command thinks I've ran it in the current directory?

If a command is using basename() or argv[0] (or whatever it's called) to get where it was called from, and it requires files in its directory, it may look in the directory that you called the application from; i.e. the wrong one.

Of course, you can cd to the directory first and invoke it there, but is there a way to invoke the application so that it thinks you've ran it from its directory (without cding first)?

If it is possible, I will use this in crontab, as the thought of using cd in there scares me.

link|improve this question

71% accept rate
feedback

2 Answers

up vote 1 down vote accepted

Why does using cd in crontab scare you?

Note that if you want to just temporarily change directory and then come back, you can use pushd and popd

link|improve this answer
I get the impression it could affect the workings of other commands that may be in there, or the cron daemon? Maybe I'm being a bit daft, like. – bean Aug 18 '11 at 13:28
And I suppose I'm not a fan of the strings of cd dir && ./cmd entries in the crontab. I just figured there would be a way to do it easily. – bean Aug 18 '11 at 13:32
2  
@panic This is actually the common way to do it. cd blah && command. – slhck Aug 18 '11 at 13:38
@panic Well, then use pushd and popd. pushd dir && ./cmd && popd – Let_Me_Be Aug 18 '11 at 14:01
feedback

You can make a script like this:

#!/bin/sh
cd /dir/
yourcommand

Then save it somewhere and add that script to crontab

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.