Restart Aplikasi secara Berkala

Adi Purnama
Terkadang, memory usage di server lebih besar dari biasanya kalau aplikasi jarang di restart. Kadang bisa sampai 70% penggunaan memory, padahal aplikasinya sedang idle.
Setelah aplikasi di-restart, penggunaan memory menjadi lebih kecil.
Kalau harus restart aplikasi manual satu persatu secara berkala, rasanya repot juga. Kita perlu suatu script yang bisa dijalankan di server pakai crontab, seperti pada chapter Manajemen Build Cache.
Seperti biasa, saya minta AI saja untuk ini. Simpan script ini ke dalam sebuah file di server, misalnya restart_all_apps.sh.
# Define log file
LOG_FILE="restart_all_apps.log"
# Get the list of all Dokku apps (skip the header line)
apps=$(dokku apps:list | tail -n +2)
# Check if there are any apps
if [ -z "$apps" ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') - No Dokku apps found." | tee -a "$LOG_FILE"
exit 1
fi
# Restart each app and log the output
for app in $apps; do
echo "$(date '+%Y-%m-%d %H:%M:%S') - Restarting $app..." | tee -a "$LOG_FILE"
dokku ps:restart "$app" > /dev/null 2>> "$LOG_FILE"
echo "$(date '+%Y-%m-%d %H:%M:%S') - Restarted $app" | tee -a "$LOG_FILE"
done
echo "$(date '+%Y-%m-%d %H:%M:%S') - All Dokku apps have been restarted." | tee -a "$LOG_FILE"
Kemudian, tambahkan ke dalam crontab.
0 17 * * * bash /path/to/restart_all_apps.sh