2

I would like to execute a command in bash every 30 seconds and have it run as a background job. Form this post, I gather that using the watch command is useful. However every time I attempt to background it, it stops.

watch -n 1 'date >> dateFile.txt'

works. I can cat dateFilet.txt and see a bunch of lines.

watch -n 1 'date >> dateFile.txt' &

returns

[1]+ Stopped watch -n 1 'date >> dateFile.txt'

executing bg returns the same line again.

What am I doing wrong? How do I fix it? What I eventually want to do is source a file every minute or so to keep certain variables in my bash session up to date.

  • Please edit your question and clarify that you need to source a file. That has a whole set of problems of its own and the valid solutions for your date example will not work. I would remove the date altogether and make the question specifically about sourcing variable definitions from a file every few seconds. – terdon Nov 5 '14 at 18:48
  • 2
    watch is curses based and doesn't have a daemon option. I'd say it's clearly not intended for this purpose (perhaps because doing this with the shell is simple enough, see sputnick's while true suggestion -- except what you actually want to do here is a complication as per terdon's comment). – goldilocks Nov 5 '14 at 18:50
  • 1
    You don't want to source a file to keep vars up to date - and you especially do not want to do that every thirty seconds. That's what the prompt does. Put the definitions in there. That's it's job. – mikeserv Nov 5 '14 at 19:12
  • @mikeserv that's a clever trick and deserves being made into an answer. It's probably the only way the OP can do what they want. – terdon Nov 5 '14 at 19:16
  • 1
    @terdon - it's no trick - that's really any prompt's primary purpose. It provides constantly re-evaluated variable information. Not all of it has to be printed every time... – mikeserv Nov 5 '14 at 19:20
4

In bash, you can do this easily using the PROMPT_COMMAND variable (from man bash):

PROMPT_COMMAND

If set, the value is executed as a command prior to issuing each primary prompt.

So, if you add something like this to your ~/.bashrc:

PROMPT_COMMAND=". /path/to/file"

You will have any variables defined in that file available.

3

In your crontab :

* * * * * date >> dateFile.txt & sleep 30; date >> dateFile.txt &

Straightforward, no ? =)

(Better put the full PATH of the date command)

Another solution:

while true; do date >> dateFile.txt & sleep 30; done
  • These are both reasonable suggestions but neither will help the OP source a file and have the variables sourced available in their current shell session. – terdon Nov 5 '14 at 18:47
  • The OP question is not that clear, I use the own OP example command – Gilles Quenot Nov 5 '14 at 18:52
  • 1
    Your answer is a good one (+1) WRT most of the question. The qualifier ("I want to source environment variables into my current shell via a background process") is not possible anyway. – goldilocks Nov 5 '14 at 18:58
  • Absolutely, I'm not saying it's your fault. That's why I also asked the OP to clarify. – terdon Nov 5 '14 at 19:14
3

What I eventually want to do is source a file every minute or so to keep certain variables in my bash session up to date.

  • You cannot do that from a child process.
  • You cannot do that from a parent process.
  • You cannot do that from any process other than the one you want it to affect.

Changing a process's environment is the same as changing a process's private memory (in fact, that's exactly what would be involved). Absolutely not allowed! That's why IPC is such an elaborate realm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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