#!/bin/bash # Function to create a cron job create_cron_job() { user=$1 # Username for the cron job minute=$2 # Minute of the hour (0-59) hour=$3 # Hour of the day (0-23) day_of_month=$4 # Day of the month (1-31) month=$5 # Month of the year (1-12) day_of_week=$6 # Day of the week (0-6, Sunday is 0) command=$7 # Command to execute # Construct the cron job line cron_line="${minute} ${hour} ${day_of_month} ${month} ${day_of_week} ${command}" # Add the cron job for the specified user. Redirects stderr to stdout. (crontab -u "$user" -l 2>/dev/null; echo "$cron_line") | crontab -u "$user" - echo "Cron job added for user $user:" echo "$cron_line" } whoami # Example usage: user="yankee" minute="20" # 4:20 PM hour="16" # 4 PM in 24-hour format day_of_month="*" # Every day of the month month="*" # Every month day_of_week="*" # Every day of the week command="/tmp/file.bash" create_cron_job "$user" "$minute" "$hour" "$day_of_month" "$month" "$day_of_week" "$command" # Example with different schedule (e.g., every Monday at 2:00 AM): # user="yankee" # minute="0" # hour="2" # day_of_month="*" # month="*" # day_of_week="1" # Monday (0 is Sunday, 1 is Monday, etc.) # command="/tmp/another_script.sh" # create_cron_job "$user" "$minute" "$hour" "$day_of_month" "$month" "$day_of_week" "$command"