How do I execute an arbitrary script with a working directory of the directory its in?
/etc/init.d
probably you are runnig (starting) that script from /etc/init.d
?
Add cd /opt/script
at the first line of the script
OR
...to keep it dynamic, add:cd "$(dirname "$0")"
How do I run a program with a different working directory from current, from Linux shell?
Call the program like this:
(cd /c; /a/helloworld)
The parentheses cause a sub-shell to be spawned. This sub-shell then changes its working directory to /c
, then executes helloworld
from /a
. After the program exits, the sub-shell terminates, returning you to your prompt of the parent shell, in the directory you started from.
Error handling: To avoid running the program without having changed the directory, e.g. when having misspelled /c
, make the execution of helloworld
conditional:
(cd /c && /a/helloworld)
Reducing memory usage: To avoid having the subshell waste memory while hello world executes, call helloworld
via exec:
(cd /c && exec /a/helloworld)
[Thanks to Josh and Juliano for giving tips on improving this answer!]
how do you call shell scripts from an arbitrary directory?
In crontab, simply use:
(cd /home && php /home/testfile.php >> /home/logFile.txt)
As for referencing your own script with cron, just use its path, like so:
* * * * * /home/myscript.sh
Remember to set the executable bit first with
chmod +x /home/myscript.sh
How to run a shell script when a file or directory changes?
Use inotify-tools.
The linked Github page has a number of examples; here is one of them.
#!/bin/sh
cwd=$(pwd)
inotifywait -mr \
--timefmt '%d/%m/%y %H:%M' --format '%T %w %f' \
-e close_write /tmp/test |
while read -r date time dir file; do
changed_abs=${dir}${file}
changed_rel=${changed_abs#"$cwd"/}
rsync --progress --relative -vrae 'ssh -p 22' "$changed_rel" \
usernam@example.com:/backup/root/dir && \
echo "At ${time} on ${date}, file $changed_abs was backed up via rsync" >&2
done
Run N Shell Scripts in Folder
find
the scripts, get the head
, then execute with xargs
.
find . -name '*.sh' | head -n 10 | xargs -n1 sh
You can run the scripts in parallel with xargs
with a simple -P0
option. You can script the xargs
with some xargs sh -c 'bash "$@" -H || exit 125' --
to make xargs
exit with nonzero status or immediately after any of the scripts fail to run or something.
If you feel like unfamiliar with xargs
, just do a simple while read
loop:
find . -name '*.sh' | head -n 10 |
while IFS= read -r script; do
bash "$script" -H || break
done
And in parallel you have to get out of the pipe subshell:
while IFS= read -r script; do
bash "$script" -H || break &
done < <(
find . -name '*.sh' | head -n 10
)
wait # for all the childs
or wait for childs in the subshell itself:
find . -name '*.sh' | head -n 10 |
{
while IFS= read -r script; do
bash "$script" -H || break &
done
wait
}
How to create a directory within a directory using shell scripting?
This can be done in a single step, since mkdir -p
will create all needed parent directories on the way (that's what the -p
option does).
#!/bin/bash
IFS=/
mkdir -p "$*"
Explanation: $*
expands to all of the script's arguments, spliced together into a single string, separated by the first character of IFS
. That's normally a space character, but here it's set to "/" instead. The double-quotes around it prevent unexpected word-splitting or wildcard expansion.
So, if you run direct.sh dir1 dir2 dir3
, it executes mkdir -p "dir1/dir2/dir3"
, which creates all 3 directory levels (or at least, those that don't already exist).
BTW, in general you should set IFS
back to normal after changing it like this. But since there's nothing afterward in the script that might get messed up, and changing it in a script doesn't affect the parent process (the shell you used to run the script), there's no need to set it back in this case.
Perform an action in every sub-directory using Bash
for D in `find . -type d`
do
//Do whatever you need with D
done
Related Topics
Linux Kernel Aio, Open System Call
Can Tmux Save Commands to a File, Like .Bash_History
How to Test My Bash Script on Older Versions of Bash
How to Setup Cron Job on Amazon Linux Ami
Docker Create Two Bridges That Corrupts My Internet Access
How to Write Kernel Space Memory (Physical Address) to a File Using O_Direct
Cmake Error: The Following Variables Are Used in This Project, But They Are Set to Notfound
Setup Sftp to Use Public-Key Authentication
The Difference Between Wait_Queue_Head and Wait_Queue in Linux Kernel
Automatically Adjusting Process Priorities Under Linux
Open-Source Opengl Profiler for Linux
How to Get Groupname When I Have The Groupid
Cross-Compilation to X86_64-Unknown-Linux-Gnu Fails on MAC Osx
Why Does Wget Output to Stderr Rather Than Stdout
Golang Os/Exec, Realtime Memory Usage
Convert Charset from a Entire Project to Utf-8
How to Compare Two Text Files for The Same Exact Text Using Bash