2

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?

  • 2
    it will run all the commands simultaniously – msp9011 Aug 29 '18 at 19:40
2

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.

  • 1
    generally, that might be the case. In practice though, running 4 parallel "find"-s or any other IO-intensive program in the same FS is not the brightest idea. Depending on the FS one may significantly alter over performance and introduce iowait. – George Ivanov Aug 29 '18 at 22:02
3

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.

1

Cron will start all 4 processes in parallel at midnight.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.