Thursday, December 29, 2022

What is Cron and Crontab in Linux

Cron is the system's main scheduler for running jobs or tasks unattended. A command called crontab allows the user to submit, edit or delete entries to cron. A crontab file is a user file that holds the scheduling information.


crontab -e


And enter the below command 


*/1 * * * * cd /Users/rk/Documents/RR/projects/tools/cron_tests && python crontest.py >> /Users/rk/Documents/RR/projects/tools/cron_tests/cron.txt 2>&1


The cron_test.py can be as below



#!/usr/bin/python3

from datetime import datetime

import os

sysDate = datetime.now()

# convert system date to string

curSysDate = (str(sysDate))

# parse the date only from the string

curDate = curSysDate[0:10]

# parse the hour only from the string

curHour = curSysDate[11:13]

# parse the minutes only from the string

curMin = curSysDate[14:16]

# concatenate hour and minute with underscore

curTime = curHour + '_' + curMin

# val for the folder name

folderName = curDate + '_' + curTime

# make a directory

os.mkdir(folderName)


Practically, there may be some problems with permissions in the Mac. Next to give permissions 


chmod 777 crontest.py 

Or chmod +x  crontest.py 


references:

https://www.geeksforgeeks.org/crontab-in-linux-with-examples/


No comments:

Post a Comment