Running a cron every 30 seconds

后端 未结 19 953
面向向阳花
面向向阳花 2020-11-22 10:56

Ok so I have a cron that I need to run every 30 seconds.

Here is what I have:

*/30 * * * * /bin/bash -l -c \'cd /srv/last_song/releases/2012030813315         


        
相关标签:
19条回答
  • 2020-11-22 11:37

    Crontab job can be used to schedule a job in minutes/hours/days, but not in seconds. The alternative :

    Create a script to execute every 30 seconds:

    #!/bin/bash
    # 30sec.sh
    
    for COUNT in `seq 29` ; do
      cp /application/tmp/* /home/test
      sleep 30
    done
    

    Use crontab -e and a crontab to execute this script:

    * * * * * /home/test/30sec.sh > /dev/null
    
    0 讨论(0)
  • 2020-11-22 11:38

    I just had a similar task to do and use the following approach :

    nohup watch -n30 "kill -3 NODE_PID" &
    

    I needed to have a periodic kill -3 (to get the stack trace of a program) every 30 seconds for several hours.

    nohup ... & 
    

    This is here to be sure that I don't lose the execution of watch if I loose the shell (network issue, windows crash etc...)

    0 讨论(0)
  • 2020-11-22 11:43

    Cron's granularity is in minutes and was not designed to wake up every x seconds to run something. Run your repeating task within a loop and it should do what you need:

    #!/bin/env bash
    while [ true ]; do
     sleep 30
     # do what you need to here
    done
    
    0 讨论(0)
  • 2020-11-22 11:46

    Use watch:

    $ watch --interval .30 script_to_run_every_30_sec.sh
    
    0 讨论(0)
  • 2020-11-22 11:48

    You can run that script as a service, restart every 30 seconds

    Register a service

    sudo vim /etc/systemd/system/YOUR_SERVICE_NAME.service
    

    Paste in the command below

    Description=GIVE_YOUR_SERVICE_A_DESCRIPTION
    
    Wants=network.target
    After=syslog.target network-online.target
    
    [Service]
    Type=simple
    ExecStart=YOUR_COMMAND_HERE
    Restart=always
    RestartSec=10
    KillMode=process
    
    [Install]
    WantedBy=multi-user.target
    

    Reload services

    sudo systemctl daemon-reload
    

    Enable the service

    sudo systemctl enable YOUR_SERVICE_NAME
    

    Start the service

    sudo systemctl start YOUR_SERVICE_NAME
    

    Check the status of your service

    systemctl status YOUR_SERVICE_NAME
    
    0 讨论(0)
  • 2020-11-22 11:49

    Why not just adding 2 consecutive command entries, both starting at different second?

    0 * * * * /bin/bash -l -c 'cd /srv/last_song/releases/20120308133159 && script/rails runner -e production '\''Song.insert_latest'\'''
    30 * * * * /bin/bash -l -c 'cd /srv/last_song/releases/20120308133159 && script/rails runner -e production '\''Song.insert_latest'\'''
    
    0 讨论(0)
提交回复
热议问题