Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

338
Vistas
How to schedule a task automatically with a while loop in bash?

I'm trying to schedule my script to run at two different times and then keep running in the background waiting for the next time that the conditions are met again.

So far I am not able to do it because after that one condition is met this scripts exits automatically:

function schedule {

while :
do
    hour=$(date +"%H") 
    minute=$(date +"%M")

    if [[ "$hour" = "02" && "$minute" = "31" ]]; then
        # run some script
        exec /home/gfx/Desktop/myscript.sh
        wait
        schedule

    elif [[ "$hour" = "02" && "$minute" = "32" ]]; then
        # run some script
        exec /home/gfx/Desktop/myscript.sh
        wait
        schedule
    fi 

done

}

schedule

The script that I execute is the following :

$cat myscript.sh
echo "this a message"

Any idea or comment are welcome, thanks!

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

When you do:

 exec some program
 something else

if the exec works, then the something else will never happen because the script is replaced by what you exec'd

Maybe instead of:

exec /home/gfx/Desktop/myscript.sh
wait
schedule

you want:

/home/gfx/Desktop/myscript.sh &
wait
# schedule  -- don't make a fork bomb

As noted in the comments, & + wait is kind of silly, you probably want:

/home/gfx/Desktop/myscript.sh &
# wait
# schedule

or:

/home/gfx/Desktop/myscript.sh
# wait
# schedule

depending on whether or not you want myscript in the background.

ALSO you should put a sleep in the loop, (maybe sleep(59)) to keep it from looping like a banshee.

You begin to understand the appeal of cron I think (at least for things you don't want to interact with your terminal session).

over 4 years ago · Santiago Trujillo Denunciar

0

Crontabs are best place to put these things.

  • Edit crontab using

    $ crontab -e

  • Add following lines to the files

31 2 * * *  /home/gfx/Desktop/myscript.sh

And save crontab file. Your script will be executed every day 2:31 am

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda