开发者

Get unix script to behave as if it was run from a different folder

I am using a scheduler to run a unix script which starts up my application. The script is in the PATH of the user used by the scheduler. Hence, can be run from an y

My application log files are created relative to where the script is run from. Unfortu开发者_开发技巧natley, the scheduler does not run the script from the folder I had hoped hence log files are not going to correct folder.

Is there any way in I get the script to run and behaves as it was run from a specified folder, e.g. ./ScriptName.sh Working_Folder | Run_Folder

Note: I cannot change the script


if your scheduler run your tasks using a shell (which it probably do) you can use { cd /log/dir ; script; } directly as command.

if not, you need to use a wrapper script as stated by @Gilles but i would do:

#!/bin/sh
cd /log/dir
exec /path/to/script "$@"

to save a little memory. The extra exec will make sure only the script interpreter is in memory instead of both (sh and the script interpreter).


If you can't change the script, you'll have to make the scheduler run a different command, not the script directly. For example, make the scheduler run a simple wrapper script.

#!/bin/sh
cd /desired/directory/for/log/files
/path/to/script "$@"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜