запуск команд цепочки функций bash в фоновом режиме

211
codyc4321

Я пытаюсь позвонить

port() { fuser -k $1/tcp; python manage.py runserver $1 ;} runproject() { cd $HOME/projects/$1 ; workon $1 ; port $2 & ; sleep 3 ; google-chrome 127.0.0.1:$2 ;} 

( portэто сервер в стиле Django). на источнике это ошибки с

cchilders:~/projects/scriptamajig [master]$ src bash: /home/cchilders/.bash_profile: line 134: syntax error near unexpected token `;' bash: /home/cchilders/.bash_profile: line 134: `runproject() { cd $HOME/projects/$1 ; workon $1 ; port $2 & ; sleep 3 ; google-chrome 127.0.0.1:$2 ;} ' 

моя google-chromeкоманда не открывается, я думаю, потому что port $2запускает сервер django и захватывает канал терминала. sleepЧасть даже не запускается после прогонов порта. Я хочу работать portв фоновом режиме и продолжать цепочку после этого; Каков синтаксис для фонового в функции (не псевдоним)? Спасибо

0

2 ответа на вопрос

1
Gordon Davisson

This isn't due to the function, it's because you're chaining multiple commands on a line with ; separating them. But & is already a command separator, and using both without a command in between (as in ... port $2 & ; sleep 3 ...) is a syntax error. Solution: either remove the redundant ;:

runproject() { cd $HOME/projects/$1 ; workon $1 ; port $2 & sleep 3 ; google-chrome 127.0.0.1:$2 ;} 

Or use line breaks as command separators, instead of ;:

runproject() { cd $HOME/projects/$1 workon $1 port $2 & sleep 3 google-chrome 127.0.0.1:$2 } 

...which I prefer, because I find it easier to read.

0
Some Linux Nerd

You can background a bash function like you would a program

imahappyfunction() { sleep 50000 } imahappyfunction &