Create a Cron Job Linux KodeKloud Engineer Task Success | sshpass script to Automate task

Ticker

6/recent/ticker-posts

Create a Cron Job Linux KodeKloud Engineer Task Success | sshpass script to Automate task

  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.


 Please Note :-  Perform the below commands based on your question  server,  user name & other details might differ . So please read task carefully before executing. All the Best 👍

Solutions :

1. With the help of sshpass no need to login on remote server.

   Install Cronie package on the remote machine

thor@jumphost ~$ sshpass -p Ir0nM@n ssh -o StrictHostKeyChecking=no tony@stapp01 "echo Ir0nM@n  | sudo -S yum install cronie -y"

Warning: Permanently added 'stapp01' (ED25519) to the list of known hosts.

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: CentOS Stream 9 - BaseOS                         29 kB/s | 6.4 kB     00:00    

CentOS Stream 9 - BaseOS                         17 MB/s | 8.8 MB     00:00    

CentOS Stream 9 - AppStream                      43 kB/s | 6.5 kB     00:00    

CentOS Stream 9 - AppStream                      19 MB/s |  25 MB     00:01    

CentOS Stream 9 - Extras packages                34 kB/s | 8.0 kB     00:00    

CentOS Stream 9 - Extras packages                31 kB/s |  19 kB     00:00    

Extra Packages for Enterprise Linux 9 - x86_64  117 kB/s |  25 kB     00:00    

Extra Packages for Enterprise Linux 9 - x86_64   26 MB/s |  20 MB     00:00    

Extra Packages for Enterprise Linux 9 openh264  5.6 kB/s | 993  B     00:00    

Extra Packages for Enterprise Linux 9 - Next -  106 kB/s |  25 kB     00:00    

Extra Packages for Enterprise Linux 9 - Next -  209 kB/s | 135 kB     00:00    

Dependencies resolved.

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

 Package               Architecture  Version                Repository     Size

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

Installing:

 cronie                x86_64        1.5.7-14.el9           baseos        118 k

Installing dependencies:

 cronie-anacron        x86_64        1.5.7-14.el9           baseos         32 k


Transaction Summary

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

Install  2 Packages


Total download size: 150 k

Installed size: 346 k

Downloading Packages:

(1/2): cronie-anacron-1.5.7-14.el9.x86_64.rpm   222 kB/s |  32 kB     00:00    

(2/2): cronie-1.5.7-14.el9.x86_64.rpm           582 kB/s | 118 kB     00:00    

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

Total                                           441 kB/s | 150 kB     00:00     

Running transaction check

Transaction check succeeded.

Running transaction test

Transaction test succeeded.

Running transaction

  Preparing        :                                                        1/1 

  Installing       : cronie-anacron-1.5.7-14.el9.x86_64                     1/2 

  Running scriptlet: cronie-anacron-1.5.7-14.el9.x86_64                     1/2 

  Installing       : cronie-1.5.7-14.el9.x86_64                             2/2 

  Running scriptlet: cronie-1.5.7-14.el9.x86_64                             2/2 

Created symlink /etc/systemd/system/multi-user.target.wants/crond.service → /usr/lib/systemd/system/crond.service.


  Verifying        : cronie-1.5.7-14.el9.x86_64                             1/2 

  Verifying        : cronie-anacron-1.5.7-14.el9.x86_64                     2/2 


Installed:

  cronie-1.5.7-14.el9.x86_64         cronie-anacron-1.5.7-14.el9.x86_64        

Complete!

thor@jumphost ~$ 


2. Start Crond services and check the status

thor@jumphost ~$ sshpass -p Ir0nM@n ssh -o StrictHostKeyChecking=no tony@stapp01 "echo Ir0nM@n | sudo -S systemctl start  crond.service && systemctl status  crond.service"

[sudo] password for tony: ● crond.service - Command Scheduler

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

     Active: active (running) since Fri 2025-08-08 07:23:23 UTC; 56ms ago

   Main PID: 2385 (crond)

      Tasks: 1 (limit: 411434)

     Memory: 996.0K

     CGroup: /docker/40fb44c64e340f4394724867a033f6e5b73907879fcede127f5f506d88176997/system.slice/crond.service

             └─2385 /usr/sbin/crond -n

Aug 08 07:23:23 stapp01.stratos.xfusioncorp.com systemd[1]: crond.service: Changed dead -> running

Aug 08 07:23:23 stapp01.stratos.xfusioncorp.com systemd[1]: crond.service: Job 295 crond.service/start finished, result=done

Aug 08 07:23:23 stapp01.stratos.xfusioncorp.com systemd[1]: Started Command Scheduler.

Aug 08 07:23:23 stapp01.stratos.xfusioncorp.com systemd[2385]: crond.service: Executing: /usr/sbin/crond -n

Aug 08 07:23:23 stapp01.stratos.xfusioncorp.com crond[2385]: (CRON) STARTUP (1.5.7)

Aug 08 07:23:23 stapp01.stratos.xfusioncorp.com crond[2385]: (CRON) INFO (Syslog will be used instead of sendmail.)

Aug 08 07:23:23 stapp01.stratos.xfusioncorp.com crond[2385]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 55% if used.)

Aug 08 07:23:23 stapp01.stratos.xfusioncorp.com crond[2385]: (CRON) INFO (running with inotify support)

Aug 08 07:23:23 stapp01.stratos.xfusioncorp.com crond[2385]: (CRON) INFO (@reboot jobs will be run at computer's startup.)

thor@jumphost ~$ 

3. Set the Cron job as per the task 

thor@jumphost ~$ sshpass -p Ir0nM@n ssh -o StrictHostKeyChecking=no tony@stapp01 "echo Ir0nM@n  | sudo -S bash -c \"crontab -l -u root 2>/dev/null; echo '*/5 * * * * echo hello > /tmp/cron_text'\" | sudo crontab -u root -"

[sudo] password for tony:

thor@jumphost ~$ 

4. Run Below  similar command to install cronie on all other app servers  

thor@jumphost ~$

sshpass -p Am3ric@ ssh -o StrictHostKeyChecking=no steve@stapp02 "echo Am3ric@  | sudo -S yum install cronie -y" 


thor@jumphost ~$

sshpass -p BigGr33n  ssh -o StrictHostKeyChecking=no banner@stapp03 "echo BigGr33n  | sudo -S yum install cronie -y" 


 5. Restart crond service on all other app servers

thor@jumphost ~$

sshpass -p Am3ric@ ssh -o StrictHostKeyChecking=no steve@stapp02 "echo Am3ric@ | sudo -S systemctl start  crond.service && systemctl status  crond.service"

thor@jumphost ~$

sshpass -p BigGr33n ssh -o StrictHostKeyChecking=no banner@stapp03 "echo BigGr33n | sudo -S systemctl start crond.service && systemctl status crond.service"


6. set cron job on all other app servers  :- kindly refer cron job as per your task

thor@jumphost ~$

sshpass -p Am3ric@ ssh -o StrictHostKeyChecking=no steve@stapp02 "echo Am3ric@  | sudo -S bash -c \"crontab -l -u root 2>/dev/null; echo '*/5 * * * * echo hello > /tmp/cron_text'\" | sudo crontab -u root -"


thor@jumphost ~$

sshpass -p BigGr33n ssh -o StrictHostKeyChecking=no banner@stapp03 "echo BigGr33n | sudo -S  bash -c \"crontab -l -u root 2>/dev/null; echo '*/5 * * * * echo hello > /tmp/cron_text'\" | sudo crontab -u root -"



 7. validate  on all  app servers

thor@jumphost ~$ sshpass -p Ir0nM@n ssh -o StrictHostKeyChecking=no tony@stapp01 "echo Ir0nM@n  | sudo -S crontab -l" 

[sudo] password for tony: */5 * * * * echo hello > /tmp/cron_text

thor@jumphost ~$ 

thor@jumphost ~$ sshpass -p Am3ric@ ssh -o StrictHostKeyChecking=no steve@stapp02 "echo Am3ric@  | sudo -S crontab -l" 

[sudo] password for steve: */5 * * * * echo hello > /tmp/cron_text

thor@jumphost ~$ 

thor@jumphost ~$ sshpass -p BigGr33n ssh -o StrictHostKeyChecking=no banner@stapp03 "echo BigGr33n  | sudo -S crontab -l" 

[sudo] password for banner: */5 * * * * echo hello > /tmp/cron_text

thor@jumphost ~$ 



8.  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

Create a Cron Job  Linux KodeKloud Engineer Task Success | sshpass script to Automate task