This first code block written in a shell script can used to trigger the copy to happen automatically after 30 minutes, every 30 minutes. This is useful if you do not have full admin permissions on the box.
#!/bin/bash
while true; do
# Run your Python script
/path/to/your/python/executable /path/to/your_script.py
# Sleep for 30 minutes
sleep 1800 # 1800 seconds = 30 minutes
done
import os
import shutil
import csv
import time
def copy_files(source_folder, destination_folder):
if not os.path.exists(destination_folder):
os.makedirs(destination_folder)
log_file = 'file_copy_log.csv'
with open(log_file, 'w', newline='') as csvfile:
csv_writer = csv.writer(csvfile)
csv_writer.writerow(['Source File', 'Destination File', 'Time Taken (seconds)'])
for filename in os.listdir(source_folder):
source_path = os.path.join(source_folder, filename)
destination_path = os.path.join(destination_folder, filename)
start_time = time.time()
try:
shutil.copy(source_path, destination_path)
except Exception as e:
print(f"Error copying file {filename}: {e}")
continue
end_time = time.time()
elapsed_time = end_time - start_time
log_data = [source_path, destination_path, elapsed_time]
csv_writer.writerow(log_data)
print(f"File {filename} copied successfully")
# Example usage
source_folder = 'path/to/source/folder'
destination_folder = 'path/to/destination/folder'
copy_files(source_folder, destination_folder)
To make changes to the cron table we need to have sudo or admin rights.
crontab -e
*/30 * * * * /path/to/python /path/to/your/script.py
Edit the cron table to include the above line, kick the script off every 30 min, this is an alternative to using the shell script at the top of this page, to run 2 versions off set by 15 min use the following entry instead
0,30 * * * * /path/to/your/myscript.sh
15,45 * * * * /path/to/your/myscript.sh
Alternatively to run the Python with out the shell script use the following
0,30 * * * * /path/to/python /path/to/your/script.py
15,45 * * * * /path/to/python /path/to/your/script.py