3

For a specific reason I have to do an infinite loop inside a function and then run the function as a daemon,

#!/bin/sh
lol(){
while true
do
    echo "looping..."
    sleep 2
done
}
lol() &

that script doesn't work, it gives me the following error:

/tmp/test: line 9: syntax error: unexpected "&"

How do I do an infinite loop inside a function in ash ?

Lin
  • 1,771
  • 6
  • 17
  • 21
  • 1
    Running the function in the background won't necessarily daemonize it, by the way. Proper daemonization calls for disconnection from the TTY, which is somewhat incompatible with writing to the TTY, as you're doing here. – Charles Duffy Mar 31 '15 at 14:49
  • (...if you just meant "background it", not "daemonize it" then carry on). – Charles Duffy Mar 31 '15 at 14:50
  • 1
    (Also, if your goal is to ensure that a service is running... don't do it this way, really. There are much, **much** better approaches; almost every operating system ships with a proper process supervision system). – Charles Duffy Mar 31 '15 at 14:51

1 Answers1

2

You're just starting the function wrong -- it's nothing to do with the loop:

lol &

Parenthesis are used only at function definition time, not for invocation.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441