-1

I do the following at my Mac terminal:

sudo for i in `seq 0 9`; do nohup my_command > log_$i.txt & done

but I receive the following error:

-bash: syntax error near unexpected token `do'

What's the problem and how can I fix it?

P.S.

I tested some of the suggestions of people here and here they are:

1)

(base) user@SERVER:/directory$ sudo for i in `seq 0 9`; do nohup my_command > log_$i.txt &; done

-bash: syntax error near unexpected token `do'

2)

(base) user@SERVER:/directory$ sudo for i in `seq 0 9`; do nohup my_command > log_$i.txt ; done

-bash: syntax error near unexpected token `do'

3)

(base) user@SERVER:/directory$ sudo for i in `seq 0 9`; do nohup my_command > log_$i.txt done

-bash: syntax error near unexpected token `do'

4)

(base) user@SERVER:directory$ sudo bash -c for i in `seq 0 9`; do nohup my_command > log_$i.txt & done

-bash: syntax error near unexpected token `do'
Cyrus
  • 84,225
  • 14
  • 89
  • 153
Outcast
  • 4,967
  • 5
  • 44
  • 99

1 Answers1

3

Running the for via sudo doesn't work as sudo expects a command. You can instead run the loop via bash:

sudo bash -c 'for i in {0..9}; do nohup command > log_$i.txt & done'

You wouldn't need to use seq command as bash has the {0..9} to support "range" loops.

See bash job control for more info on & (which puts the "job" - the command you run - in the background).

P.P
  • 117,907
  • 20
  • 175
  • 238
  • So without the & you mean like that: sudo bash -c 'for i in {0..9}; do nohup command > log_$i.txt done' ? – Outcast Apr 17 '20 at 19:44
  • Yes but you'd need `;` if `&` isn't used: `sudo bash -c 'for i in {0..9}; do nohup command > log_$i.txt; done'` – P.P Apr 17 '20 at 19:48
  • ah ok - I did it and it runs but I do not see the ids of the processes being generated (unless I am missing sth) – Outcast Apr 17 '20 at 19:52
  • Ah, you'd need `&` - sorry for the confusion! The shell prints the pids when running in background. Also for the loop to proceed (instead of waiting for the previous command to finish). – P.P Apr 17 '20 at 19:55
  • Ok no worries, just asking to learn things correctly too - anyway I am very amateur on these things :) – Outcast Apr 17 '20 at 19:57
  • I have removed the confusing part from the answer now. – P.P Apr 17 '20 at 19:57
  • By the way, are you sure that this runs properly as I want? Because my impression is that the one loop waits for the previous process started by the previous loop to be completed before it starts and this is not what I want. I want simply to start 10 (nohup) processes at the same time with one command instead of doing 10 different nohup. Is the for loop the right option for what I want or another one? – Outcast Apr 17 '20 at 21:55
  • Yes, it'd start 10 processes at the same time without waiting for the previous nohup command. You can easily test with `sleep` command. For example, `sudo bash -c 'for i in {0..9}; do nohup sleep 60 & done'` then if you run `ps -eaf | grep sleep`, you should see 10 sleep processes. – P.P Apr 17 '20 at 21:59
  • I had done this: `ps -ef --sort=start_time` and I am 100% sure that I saw all the processes in the list; but they just have been only initialised and not run in parallel? It is just that my results allude that only the first loop run but I may be wrong it may be that all the loops run but all stopped together at some point (?). (To give you even more context each nohup process out of the 10 was supposed to generate around 5k files but finally only 5k files were generated so this is I started to think if only the 1st loop run). – Outcast Apr 17 '20 at 22:02
  • Regarding multiple nohup, I have found this: https://unix.stackexchange.com/questions/67221/running-multiple-nohup-commands-in-background. But this means that basically I have to concatenate all my (long) commands in one strings which is not elegant either than having 10 separate nohup and this is why I thought of the for loop. – Outcast Apr 17 '20 at 22:03
  • The loop as written in the answer would start all 10 commands simultaneously. No doubt about that. Perhaps the "command" itself sees that another instance is running and thus waits? Because I see no other reason for all of them to not simultaneously. – P.P Apr 17 '20 at 22:06
  • Actually apologies for taking your time but I think that it runs fine. I simply checked the results at a moment where the files were around 5k and I made this thought but now they are actually 6k and it is quite evident from the `ps -ef --sort=start_time` at the `TIME` column that all the processes run for the same time. Apologies and thanks again :) – Outcast Apr 17 '20 at 22:13
  • I see, no worries :) – P.P Apr 17 '20 at 22:14