-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrontab.sh
executable file
·42 lines (42 loc) · 1.69 KB
/
crontab.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env bash
# IMPORTANT! this file works only inside Symfony project directory or sub directories
# Execute this file: ./crontab.sh in public working directory (PWD)
# give it execute permissions: chmod +x ./crontab.sh
# Find php binary path
PHP=$(command -v php)
# Get public working directory
ST_CURRENT_PATH="$(pwd)"
# Search for Symfony console absolute path
DIRECTORY_TO_FIND="bin"
# Init var to empty string
PATH_TO_SF_CONSOLE=""
# Loop in pwd to find "bin" directory
while true; do
# Try to find "bin" directory
if [[ -d "$ST_CURRENT_PATH/$DIRECTORY_TO_FIND" ]]; then
PATH_TO_SF_CONSOLE="$ST_CURRENT_PATH/$DIRECTORY_TO_FIND/console"
fi
if [[ $ST_CURRENT_PATH =~ ^/?[^/]+$ ]]; then
break
fi
ST_CURRENT_PATH="${ST_CURRENT_PATH%/*}"
done
# Exit if path to symfony console is not found
[[ -z "$PATH_TO_SF_CONSOLE" ]] && exit
# Use absolute path to php binary and symfony console run command correctly
ST_CALL_SYMFONY_CONSOLE="$PHP $PATH_TO_SF_CONSOLE"
# Use "eval" to test command
ST_DELETE_UNUSED_IMAGE_COMMAND="app:delete-unused-image --call=automatic --category=all --temporary=1 --regexmode=1 --timelimit=86400"
# Define a cron job: once a day at midnight without email
ST_CRON_JOB1="0 0 * * * $ST_CALL_SYMFONY_CONSOLE $ST_DELETE_UNUSED_IMAGE_COMMAND 2>&1"
# Append cronjob in crontab
# First create crontab for current user if it does not exist (can emit state for use priviledge or crontab existence)
crontab -u "$USER" -e
# Write out current crontab in mycronjobs temp file
crontab -l > mycronjobs
# Echo new cron into mycronjobs temp file
echo "$ST_CRON_JOB1" >> mycronjobs
# Install new mycronjobs temp file and remove it after
crontab mycronjobs
rm mycronjobs
exit