Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I used the crontab to execute a script, while the script had to do some jobs in the background in this format:

for i in {1..100}
do
    do_job &
done

However, the do_job won't be executed by crontab with &, how to fix this problem.My system is Suse.

share|improve this question

migrated from stackoverflow.com Dec 29 '12 at 17:16

This question came from our site for professional and enthusiast programmers.

1  
Crontab just spawns a shell and has no idea what the shell is running, so probably the problem is elsewhere. Does the script itself even execute? –  Thomas Dec 29 '12 at 12:43
1  
cron is not for running things in the background like this. –  Ignacio Vazquez-Abrams Dec 29 '12 at 12:43
    
@lgnacio Is there any solutions? –  cheneydeng Dec 29 '12 at 12:44
    
@Thomas Yes, the script was executed. –  cheneydeng Dec 29 '12 at 12:44
3  
Your script contains Bash syntax, does the script file have a valid #!/bin/bash shebang line? Does it fail if you run sh scriptname from the prompt? Cron jobs run with sh unless you explicitly specify Bash. –  tripleee Dec 29 '12 at 12:57

2 Answers 2

You could do something like:

nohup script.sh 1> /tmp/script.log 2> /tmp/script.err &

or use screen (deattached)

screen -d -m -S script script.sh;

to background processes (see)

share|improve this answer

I think this should work for you:

bash -c "do_job &"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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