如何使用其所在目录的工作目录执行任意脚本?

Ste*_*all 8 linux bash groovy

我需要从bash执行一个groovy脚本文件,我需要脚本有一个它所在目录的工作目录.

也就是说,在我的bash脚本中,我这样做:

/opt/script/myscript.groovy &
Run Code Online (Sandbox Code Playgroud)

但这似乎将工作目录设置为/etc/init.d我正在调用的目录.如何将该脚本的工作目录更改为/opt/script

Dav*_*iño 17

如果在/etc/init.d脚本中使用start-stop-daemon,则可以利用-d参数来实现此目的:

   -d, --chdir path
          Chdir to path before starting the process. This is done after the chroot if the -r|--chroot option is set. When not specified, start-stop-daemon will chdir to the root directory before starting the process.
Run Code Online (Sandbox Code Playgroud)


The*_*ndr 9

/etc/init.d

可能你是runnig(开始)那个脚本来自/etc/init.d

cd /opt/script在脚本的第一行添加

要么

...为了保持动态,添加: cd "$(dirname "$0")"