A command can be scheduled to run periodically by cron.
I was wondering how we can run a command in background periodically in a given interactive bash shell?
I would like to run the command from a given interactive bash shell, instead of other shells that are running, because I need the command to generate output based on the state of the shell, e.g. output of
jobsin the given shell.
I would like to run the command in background, because I would like to be able to continue to run other commands in the same shell
the command may be a script, and to let the script access the state of a given shell, I might need to
sourcethe script in the shell.
I wrote an example of such a script:
$ pwd /tmp/test $ cat test.sh #! /bin/bash while true; do dirs -l -p > /tmp/test/dirs jobs > /tmp/test/jobs sleep 5 done
and then run it in an interactive bash shell:
$ source test.sh &
but the files which get up-to-date output of the script don't contain the output of
dirs -l -p which are directly run in the given shell:
$ evince & $ jobs - Running source test.sh & + Running evince & $ cat /tmp/test/jobs $ $ pushd /tmp/ /tmp /tmp/test $ dirs -l -p /tmp/test /tmp $ cat /tmp/test/dirs /tmp/test
I was wondering why? How can we accomplish the task at the beginning of the post?