I've created the following cron for daily and weekly scripts for a minimal Nginx server environment (as part of my larger script).

The daily cronjob updates WordPress and brings back original permissions (that are changed by WordPress in its update). The weekly task clears accumulated WordPress cache, make local backups, and delete old backups.

I wonder where this script could be shortened.

cat <<-"CRON_DAILY" > /etc/cron.daily/nses_cron_daily
    for dir in /var/www/html/*/; do cd "$dir" && /usr/local/bin/wp plugin update --all --allow-root; done
    for dir in /var/www/html/*/; do cd "$dir" && /usr/local/bin/wp core update --allow-root; done
    for dir in /var/www/html/*/; do cd "$dir" && /usr/local/bin/wp theme update --all --allow-root; done
    chown www-data:www-data /var/www/html/* -R
    find /var/www/html/* -type d -exec chmod 755 {} \;
    find /var/www/html/* -type f -exec chmod 644 {} \;
chmod +x /etc/cron.daily/nses_cron_daily

# ---------------------------------------------------------------------------------------------------- #

cat <<-"CRON_WEEKLY" >> /etc/cron.weekly/nses_cron_weekly
    for dir in /var/www/html/*/wp-content; do cd "$dir" && cd cache && rm -rf *; done;

    zip -r /root/backups/dirs/html-$(date +\%F-\%T).zip /var/www/html
    find /root/backups/dirs/* -mtime +30 -exec rm {} \;
    mysqldump -u root -pPASSWORD --all-databases > /root/backups/db/db-$(date +\%F-\%T).sql
    find /root/backups/db/* -mtime +30 -exec rm {} \;
chmod +x /etc/cron.weekly/nses_cron_weekly
echo "Change DB root password in crontab."
  • \$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. \$\endgroup\$ Jan 11, 2018 at 11:07

1 Answer 1


As mentioned by cas in the discussion, you can simplify your loops:

for dir in /var/www/html/*/; do
    if pushd "$dir"; then
        /usr/local/bin/wp plugin update --all --allow-root &&
        /usr/local/bin/wp core update --allow-root &&
        /usr/local/bin/wp theme update --all --allow-root

You can also “simplify” the chmod calls:

find /var/www/html/* -exec chmod a-x,a=rX,u+w {} \+

This processes directories and files in one go, and calls chmod as few times as possible (\+ instead of \;). Using the recursive flag to chmod means you can also drop find:

chmod -R a-x,a=rX,u+w /var/www/html/*

In the weekly job,

for dir in /var/www/html/*/wp-content; do cd "$dir" && cd cache && rm -rf *; done;

should be doable as

for dir in /var/www/html/*/wp-content/cache/; do rm -rf "${dir}/*"; done

instead, or even

rm -rf /var/www/html/*/wp-content/cache/*

The \+ comment regarding find is also relevant here.

I highly recommend using “proper” backup tools instead of ad hoc backups (but it’s great to have backups, whatever they are). In particular, automysqlbackup is packaged in Debian and works great for MySQL backups, with automatic pruning and nice password handling; I find BorgBackup great for file archiving.

As cas also tried to suggest, you could do away with the heredocs and perhaps make life simpler for yourself by retrieving the scripts directly. One way to do this would be to store them separately, alongside nses.sh, and retrieve them either as part of a git clone or using curl.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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