is it ok to run cron job with the same time?

5,804

Solution 1

Yes, it is perfectly acceptable to have cron schedule multiple jobs at the same time.

Computers do nothing simultaneously, however, and they will be started in the order present in the cron table. However, they will not be run in sequence; they will be started one after the other within a few milliseconds of midnight -- simultaneously for all practical purposes.

Solution 2

Cron would start all the jobs pretty much at the same time, and they would execute concurrently.

For cleanup jobs like these though, you'd be better off writing a short shell script to run the procedure in a sequential manner. This would prevent the multiple find runs from slowing each other down (and making disk access on the rest of the system potentially a bit sluggish). It would also make it easy to change/update the cleanup procedure (for example, adding new directories or modifying the find criteria etc.) without having to modify its cron specification (or add more cleanup jobs).

For example,

#!/bin/bash

for dir in /var/log/{ambari-metrics-collector,k0,POE,REW}; do
    [ ! -d "$dir" ] && continue
    find  "$dir" -type f -mtime +10 -regex '.*\.log.*[0-9]$' -delete
done

Or just

#!/bin/bash

find  /var/log/{ambari-metrics-collector,k0,POE,REW} -type f -mtime +10 -regex '.*\.log.*[0-9]$' -delete

(which would also produce errors if one or several of those directories does not exist, which may or may not be useful to know)

You would then schedule the running of this script using cron in whatever way is appropriate on your system.


Slightly fancier:

#!/bin/bash

# directories to clean up
dirs=( /var/log/ambari-metrics-collector
       /var/log/k0
       /var/log/POE
       /var/log/REW
)

for dir in "${dirs[@]}"; do
    if [ ! -d "$dir" ]; then
        printf 'Dir "%s" does not exist, check this!\n' "$dir" >&2
    else
        printf 'Removing files from "%s":\n' "$dir"
        find  "$dir" -type f -mtime +10 -regex '.*\.log.*[0-9]$' -print -delete
    fi
done

Or, scrap this approach entirely and use logrotate or similar software and do your logfile maintenance properly instead.

Solution 3

Cron will start all 4 processes in parallel at midnight.

Share:
5,804

Related videos on Youtube

yael
Author by

yael

Updated on September 18, 2022

Comments

  • yael
    yael over 1 year

    we want to delete all files of the following job with the same time on midnight

    0 0 * * * root  [[ -d /var/log/ambari-metrics-collector ]] && find  /var/log/ambari-metrics-collector  -type f -mtime +10 -regex '.*\.log.*[0-9]$' -delete 
    
    0 0 * * * root  [[ -d /var/log/kO ]] && find  /var/log/Ko  -type f -mtime +10 -regex '.*\.log.*[0-9]$' -delete 
    
    0 0 * * * root  [[ -d /var/log/POE ]] && find  /var/log/POE  -type f -mtime +10 -regex '.*\.log.*[0-9]$' -delete  
    
    0 0 * * * root  [[ -d /var/log/REW ]] && find  /var/log/REW  -type f -mtime +10 -regex '.*\.log.*[0-9]$' -delete 
    

    is it ok to run all then on the same time?

    dose cron job will run them step by step? or both all them on the same thread?

    • Siva
      Siva over 5 years
      it will run all the commands simultaniously