-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b6ddcba
Showing
3 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Run command at intervals | ||
|
||
This bash script allows you to run commands at specified intervals, with optional sleep prevention. | ||
|
||
## Features | ||
- Run any command at regular intervals | ||
- Prevent system sleep during command execution (macOS and Linux) | ||
- Log command output and execution times | ||
|
||
## Usage | ||
|
||
```bash | ||
./run_in_intervals.sh [--interval=MINUTES] [--no-sleep] COMMAND | ||
``` | ||
|
||
|
||
### Options | ||
|
||
- `--interval=MINUTES`: Set the interval between command executions (default: 30 minutes) | ||
- `--no-sleep`: Prevent system sleep during command execution (requires `caffeinate` on macOS or `caffeine` on Linux) | ||
- `COMMAND`: The command to be executed at each interval | ||
|
||
### Examples | ||
|
||
Run a backup script every hour: | ||
|
||
```bash | ||
./run_in_intervals.sh --interval=60 --no-sleep "backup_script.sh" | ||
``` | ||
|
||
Check for updates every 15 minutes, preventing sleep: | ||
|
||
```bash | ||
./run_in_intervals.sh --interval=15 --no-sleep "check_for_updates.sh" | ||
``` | ||
|
||
Update apt packages every 15 minutes, preventing sleep: | ||
|
||
```bash | ||
./run_in_intervals.sh --interval=15 --no-sleep "apt update && apt upgrade -y" | ||
``` | ||
|
||
|
||
## Log File | ||
|
||
The script logs all command outputs and execution times to `run_in_intervals.log` in the same directory. | ||
|
||
## Notes | ||
|
||
- The script runs continuously until manually stopped | ||
- For Linux systems, it will attempt to install `caffeine` if not present | ||
- Ensure the script has execute permissions: `chmod +x run_in_intervals.sh` | ||
- The `--no-sleep` option requires `caffeinate` on macOS or `caffeine` on Linux. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#!/bin/bash | ||
|
||
# Default values | ||
INTERVAL_MINUTES=30 | ||
USE_CAFFEINATE=false | ||
COMMAND="" | ||
LOG_FILE="run_in_intervals.log" | ||
|
||
usage() { | ||
echo "Usage: $0 [--interval=MINUTES] [--no-sleep] COMMAND" | ||
echo " --interval=MINUTES Set the interval between command executions (default: 30)" | ||
echo " --no-sleep Prevent system sleep during command execution" | ||
echo " COMMAND The command to run at specified intervals" | ||
exit 1 | ||
} | ||
|
||
check_caffeinate() { | ||
if ! command -v caffeinate &> /dev/null; then | ||
if [[ "$OSTYPE" == "linux-gnu"* ]]; then | ||
echo "caffeinate is not available on Linux. Installing 'caffeine' as an alternative..." | ||
if command -v apt-get &> /dev/null; then | ||
sudo apt-get update && sudo apt-get install -y caffeine | ||
elif command -v dnf &> /dev/null; then | ||
sudo dnf install -y caffeine | ||
elif command -v yum &> /dev/null; then | ||
sudo yum install -y caffeine | ||
else | ||
echo "Error: Unable to install caffeine. Please install it manually." | ||
echo "The --no-sleep option will be ignored." | ||
USE_CAFFEINATE=false | ||
fi | ||
else | ||
echo "Warning: caffeinate or equivalent is not available on this system. The --no-sleep option will be ignored." | ||
USE_CAFFEINATE=false | ||
fi | ||
fi | ||
} | ||
|
||
run_command() { | ||
if $USE_CAFFEINATE; then | ||
if [[ "$OSTYPE" == "darwin"* ]] || [[ "$OSTYPE" == "linux-gnu"* ]]; then | ||
caffeinate $COMMAND 2>&1 | tee -a "$LOG_FILE" | ||
else | ||
eval "$COMMAND" 2>&1 | tee -a "$LOG_FILE" | ||
fi | ||
else | ||
eval "$COMMAND" 2>&1 | tee -a "$LOG_FILE" | ||
fi | ||
} | ||
|
||
run_job() { | ||
local job_interval=$1 | ||
while true; do | ||
echo "$(date): Running command" | tee -a "$LOG_FILE" | ||
run_command 2>&1 | tee -a "$LOG_FILE" & | ||
echo "$(date): Command started in background. Sleeping for $job_interval minutes" | tee -a "$LOG_FILE" | ||
sleep $((job_interval * 60)) | ||
done | ||
} | ||
|
||
parse_arguments() { | ||
while [[ $# -gt 0 ]]; do | ||
case $1 in | ||
--interval=*) | ||
INTERVAL_MINUTES="${1#*=}" | ||
if ! [[ "$INTERVAL_MINUTES" =~ ^[0-9]+$ ]]; then | ||
echo "Error: Invalid interval. Please provide a positive integer." | ||
usage | ||
fi | ||
shift | ||
;; | ||
--no-sleep) | ||
USE_CAFFEINATE=true | ||
shift | ||
;; | ||
-h|--help) | ||
usage | ||
;; | ||
*) | ||
if [ -z "$COMMAND" ]; then | ||
COMMAND="$1" | ||
else | ||
COMMAND="$COMMAND $1" | ||
fi | ||
shift | ||
;; | ||
esac | ||
done | ||
|
||
# Validate that a command was provided | ||
if [ -z "$COMMAND" ]; then | ||
echo "Error: No command provided." | ||
usage | ||
fi | ||
} | ||
|
||
main() { | ||
parse_arguments "$@" | ||
|
||
# Check for caffeinate if USE_CAFFEINATE is true | ||
if $USE_CAFFEINATE; then | ||
check_caffeinate | ||
fi | ||
|
||
run_job "$INTERVAL_MINUTES" | ||
} | ||
|
||
main "$@" |