Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

610
Views
Espere hasta que el servicio comience en bash-script

Tengo un script bash que inicia algún servicio en segundo plano. Después de que este servicio se inicia con éxito, imprime "El servidor está activo" en la salida estándar. Necesito esperar hasta que aparezca esta cadena y luego continuar ejecutando mi script. ¿Cómo puedo conseguir esto?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Yo haría de esta manera.

 ./server > /tmp/server-log.txt & sleep 1 while ! grep -m1 'Server is active' < /tmp/server-log.txt; do sleep 1 done echo Continue

Aquí -m1 le dice a grep(1) que se cierre en la primera coincidencia.

Verifiqué mi respuesta con mi "servicio" de juguete a continuación:

 #! /bin/bash trap "echo 'YOU killed me with SIGPIPE!' 1>&2 " SIGPIPE rm -f /tmp/server-output.txt for (( i=0; i<5; ++i )); do echo "i==$i" sleep 1; done echo "Server is active" for (( ; i<10; ++i )); do echo "i==$i" sleep 1; done echo "Server is shutting down..." > /tmp/server-output.txt

Si reemplaza echo Continue con echo Continue; sleep 1; ls /tmp/server-msg.txt , verá ls: cannot access /tmp/server-output.txt: No such file or directory que demuestre que la acción "Continuar" se activó justo después de que la salida del Server is active .

over 4 years ago · Santiago Trujillo Report

0

Para que yo lea el estado del servicio en cuanto a la aplicación de service :

 $ /sbin/service network status network.service - Network Connectivity Loaded: loaded (/lib/systemd/system/network.service; enabled) Active: active (exited) since Ср 2014-01-29 22:00:06 MSK; 1 day 15h ago Process: 15491 ExecStart=/etc/rc.d/init.d/network start (code=exited, status=0/SUCCESS) $ /sbin/service httpd status httpd.service - SYSV: Apache is a World Wide Web server. It is used to serve HTML files and CGI. Loaded: loaded (/etc/rc.d/init.d/httpd) Active: activating (start) since Пт 2014-01-31 13:59:06 MSK; 930ms ago

y se puede hacer con el código:

 function is_in_activation { activation=$(/sbin/service "$1" status | grep "Active: activation" ) if [ -z "$activation" ]; then true; else false; fi return $?; } while is_in_activation network ; do true; done
over 4 years ago · Santiago Trujillo Report

0

Utilice grep -q . La opción -q hace que grep sea silencioso y se cerrará inmediatamente cuando aparezca el texto.

El siguiente comando inicia ./some-service en segundo plano y se bloquea hasta que aparece "Server is active" en la salida estándar.

 (./some-service &) | grep -q "Server is active"
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!