Sunday, June 11, 2017

How to create cron job in ubuntu

In this POST I will show how to create a cron job in Ubuntu. For this we will use python script to print current date and time in log file.

In the previous posts I had shown How to....

2017-06-10 21:35:01.655477
2017-06-10 21:36:01.686346
2017-06-10 21:37:01.711900
2017-06-10 21:38:01.740353
2017-06-10 21:39:01.767497
2017-06-10 21:40:01.792622
2017-06-10 21:41:01.823149

Lets start by creating file. copy and paste the below code into the created file.

import datetime

def show_datetime():
Now run this python file in the terminal to check expected output.

python home/vara/ 
Now to Allow this file as Executing program

sudo chmod +x home/vara/
Now In the Terminal open cron job with the below command.

sudo crontab -e
Now Copy paste the below code in the cron tab accordingly and save it.

* * * * * /home/vara/ >> /home/vara/showtime.log 2>&1
The above cronjob will execute automatically for every 1min and writes the out put in the log file.

Here You may get the error as shown

import: unable to open X server `' @ error/import.c/ImportImageCommand/364.
/home/vara/ 5: /home/vara/ Syntax error: "(" unexpected

To resolve the above error just add #!/usr/bin/env python in 1st line of python file.

#!/usr/bin/env python

import datetime

def show_datetime():

No comments:

Post a Comment