Create a Cron Job Linux Server

Ticker

6/recent/ticker-posts

Create a Cron Job Linux Server

 Question :  The Nautilus system admins team has prepared scripts to automate several day-to-day tasks. They want them to be deployed on all app servers in Stratos DC on a set schedule. Before that they need to test similar functionality with a sample cron job. Therefore, perform the steps below:

a. Install cronie package on all Nautilus app servers and start crond service.

b. Add a cron */5 * * * * echo hello > /tmp/cron_text for root user.


Solution:  

1. At first login to all App server  & Switch to  root user

thor@jump_host ~$ ssh tony@stapp01

The authenticity of host 'stapp01 (172.16.238.10)' can't be established.

ECDSA key fingerprint is SHA256:HfSF2lgWTKxzOCOIseoaLzUMcUFiflwRYS+5VfEeADA.

ECDSA key fingerprint is MD5:90:8c:06:3d:71:b2:de:80:4d:45:e2:59:45:77:d2:7c.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added 'stapp01,172.16.238.10' (ECDSA) to the list of known hosts.

tony@stapp01's password:

[tony@stapp01 ~]$ sudo su -

 We trust you have received the usual lecture from the local System

Administrator. It usually boils down to these three things:

     #1) Respect the privacy of others.

    #2) Think before you type.

    #3) With great power comes great responsibility.

 [sudo] password for tony:

[root@stapp01 ~]#

2. Install cronie package on server 

[root@stapp01 ~]# yum install cronie -y

Loaded plugins: fastestmirror, ovl

Determining fastest mirrors

 * base: mirror.genesisadaptive.com

 * extras: mirrors.gigenet.com

 * updates: centos.mirror.lstn.net

base                                                                                         | 3.6 kB  00:00:00    

extras                                                                                       | 2.9 kB  00:00:00    

updates                                                                                      | 2.9 kB  00:00:00    

(1/4): base/7/x86_64/group_gz                                                                | 153 kB  00:00:00    

(2/4): extras/7/x86_64/primary_db                                                            | 242 kB  00:00:00    

(3/4): updates/7/x86_64/primary_db                                                           | 8.8 MB  00:00:00    

(4/4): base/7/x86_64/primary_db                                                              | 6.1 MB  00:00:00    

Resolving Dependencies

--> Running transaction check

---> Package cronie.x86_64 0:1.4.11-23.el7 will be installed

--> Processing Dependency: dailyjobs for package: cronie-1.4.11-23.el7.x86_64

--> Running transaction check

---> Package cronie-anacron.x86_64 0:1.4.11-23.el7 will be installed

--> Processing Dependency: crontabs for package: cronie-anacron-1.4.11-23.el7.x86_64

--> Running transaction check

---> Package crontabs.noarch 0:1.11-6.20121102git.el7 will be installed

--> Finished Dependency Resolution

 Dependencies Resolved

 ====================================================================================================================

 Package                      Arch                 Version                                 Repository          Size

====================================================================================================================

Installing:

 cronie                       x86_64               1.4.11-23.el7                           base                92 k

Installing for dependencies:

 cronie-anacron               x86_64               1.4.11-23.el7                           base                36 k

 crontabs                     noarch               1.11-6.20121102git.el7                  base                13 k

 Transaction Summary

====================================================================================================================

Install  1 Package (+2 Dependent packages)

 Total download size: 141 k

Installed size: 260 k

Downloading packages:

(1/3): cronie-anacron-1.4.11-23.el7.x86_64.rpm                                               |  36 kB  00:00:00    

(2/3): cronie-1.4.11-23.el7.x86_64.rpm                                                       |  92 kB  00:00:00    

(3/3): crontabs-1.11-6.20121102git.el7.noarch.rpm                                            |  13 kB  00:00:00    

--------------------------------------------------------------------------------------------------------------------

Total                                                                               579 kB/s | 141 kB  00:00:00    

Running transaction check

Running transaction test

Transaction test succeeded

Running transaction

  Installing : cronie-anacron-1.4.11-23.el7.x86_64                                                              1/3

  Installing : cronie-1.4.11-23.el7.x86_64                                                                      2/3

  Installing : crontabs-1.11-6.20121102git.el7.noarch                                                           3/3

  Verifying  : cronie-1.4.11-23.el7.x86_64                                                                      1/3

  Verifying  : cronie-anacron-1.4.11-23.el7.x86_64                                                              2/3

  Verifying  : crontabs-1.11-6.20121102git.el7.noarch                                                           3/3

 Installed:

  cronie.x86_64 0:1.4.11-23.el7                                                                                    

 Dependency Installed:

  cronie-anacron.x86_64 0:1.4.11-23.el7                   crontabs.noarch 0:1.11-6.20121102git.el7                 

 Complete!

[root@stapp01 ~]#

3.  Start cron service & check the  status 

[root@stapp01 ~]# systemctl start crond.service

[root@stapp01 ~]# systemctl status crond.service

● crond.service - Command Scheduler

   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)

   Active: active (running) since Wed 2021-06-23 09:48:40 UTC; 7s ago

 Main PID: 867 (crond)

   CGroup: /docker/1e182a61c844a4126c21ead9f17af0fd695bfb11e177c1b7d3be38d07ec49c8a/system.slice/crond.service

           └─867 /usr/sbin/crond -n

 

Jun 23 09:48:40 stapp01.stratos.xfusioncorp.com systemd[1]: About to execute: /usr/sbin/crond -n $CRONDARGS

Jun 23 09:48:40 stapp01.stratos.xfusioncorp.com systemd[1]: Forked /usr/sbin/crond as 867

Jun 23 09:48:40 stapp01.stratos.xfusioncorp.com crond[867]: (CRON) INFO (Syslog will be used instead of sendmail.)

Jun 23 09:48:40 stapp01.stratos.xfusioncorp.com systemd[1]: crond.service changed dead -> running

Jun 23 09:48:40 stapp01.stratos.xfusioncorp.com crond[867]: (CRON) INFO (RANDOM_DELAY will be scaled with fact...d.)

Jun 23 09:48:40 stapp01.stratos.xfusioncorp.com systemd[1]: Job crond.service/start finished, result=done

Jun 23 09:48:40 stapp01.stratos.xfusioncorp.com crond[867]: (CRON) INFO (running with inotify support)

Jun 23 09:48:40 stapp01.stratos.xfusioncorp.com systemd[1]: Started Command Scheduler.

Jun 23 09:48:40 stapp01.stratos.xfusioncorp.com systemd[867]: Executing: /usr/sbin/crond -n

Hint: Some lines were ellipsized, use -l to show in full.

[root@stapp01 ~]#


4.  Create a cronjob  as per the task for root user
    

[root@stapp01 ~]# crontab -e

no crontab for root - using an empty one

crontab: installing new crontab

[root@stapp01 ~]#

 
5.  Check cron job for user root

[root@stapp01 ~]# crontab -l

*/5 * * * * echo hello > /tmp/cron_text

[root@stapp01 ~]#


6.  Validate  cron_text file is created successfully, before that done finish the task

[root@stapp01 ~]# ll /tmp/

total 8

-rw-r--r-- 1 root root   6 Jun 23 09:55 cron_text

-rwx------ 1 root root 836 Aug  1  2019 ks-script-rnBCJB

-rw------- 1 root root   0 Aug  1  2019 yum.log

[root@stapp01 ~]#


Please Note :- I have showed only for stapp01
You have to do this in all app server stapp01,stapp02, stapp03. 

7.  Click on Finish & Confirm to complete the task successful

Happy Learning!!!!


Apart from this if you need more clarity,  I have made a  tutorial video on this , please go through and share your comments. Like and share the knowledge



Post a Comment

0 Comments

Latest Posts

KodeKloud Kubernetes Security CKS  Lab Challenge 4 |  Audit-policy | Install & configure falco utility | Inspect the API server audit logs and identify the user