This discussion is archived
4 Replies Latest reply: Dec 10, 2013 2:45 AM by Dude! RSS

Run cronjob on remote server from local machine

Tushar Lapani Newbie
Currently Being Moderated

Hi All!!

I don't wan write script ( cron job) on remote machine. Instead, I want to schedule a cron job on my local machine that run for the remote server. Both machine has same linux version Redhat 5.5 and I have ssh configured on both remote server and local machine.  So I want to know is there any linux scripting or any command .

 

Thanks & Regards

Tushar Lapani

  • 1. Re: Run cronjob on remote server from local machine
    EdStevens Guru
    Currently Being Moderated

    TusharLapani wrote:

     

    Hi All!!

    I don't wan write script ( cron job) on remote machine. Instead, I want to schedule a cron job on my local machine that run for the remote server.

     

    Why?  What is the business problem you are trying to solve by doing it this way?

     

     

    Both machine has same linux version Redhat 5.5 and I have ssh configured on both remote server and local machine.  So I want to know is there any linux scripting or any command .

     

    Thanks & Regards

    Tushar Lapani

  • 2. Re: Run cronjob on remote server from local machine
    Dude! Guru
    Currently Being Moderated

    To use ssh to execute commands on a remote machine is fairly simply and does not even require that your host and remote host use the same OS or user.

     

    For instance:

     

    Saturn:~ bb$ uname -s -r

    Darwin 12.5.0

    Saturn:~ bb$ ssh dude@10.0.2.5 "uname -sr"

    dude@10.0.2.5's password:

    Linux 3.8.13-16.2.1.el6uek.x86_64

     

    If you want to do this in cron you will have to setup ssh user equivalence, so it does not prompt you for password.

     

    To use the example above, where 10.0.2.5 is the remote machine, for instance:

     

    Create public and private keys on the current host:

     

    Saturn:~ bb$ mkdir -p ~/.ssh

    Saturn:~ bb$ chmod 700 ~/.ssh

    Saturn:~ bb$ rm -f ~/.ssh/id_dsa

    Saturn:~ bb$ ssh-keygen -t dsa -N "" -f ~/.ssh/id_dsa

    Generating public/private dsa key pair.

    Your identification has been saved in /Users/bb/.ssh/id_dsa.

    Your public key has been saved in /Users/bb/.ssh/id_dsa.pub.

    The key fingerprint is:

    b4:6d:1c:20:d9:63:83:fd:59:c1:5d:e8:c6:8d:f2:a2 bb@Saturn.local

    The key's randomart image is:

    +--[ DSA 1024]----+

    |   o.o ..o .     |

    |  . o+. . =      |

    | .  o.+. + =     |

    |  ....Eo  o .    |

    |  .o .. S        |

    |   ..  .         |

    |    . o          |

    |    .o           |

    |   .o.           |

    +-----------------+

     

    Create the necessary .ssh directory on the remote host and set required privileges:

     

    Saturn:~ bb$ ssh dude@10.0.2.5 "mkdir -p .ssh; chmod 700 .ssh"

    root@10.0.2.5's password:

     

    Copy the DSA public key to the remote host account and set required privileges:

     

    Saturn:~ bb$ KEY=`cat ~/.ssh/id_dsa.pub`

    Saturn:~ bb$ ssh dude@10.0.2.5 "echo "$KEY" >> .ssh/authorized_keys; chmod 644 .ssh/authorized_keys"

    dude@10.0.2.5's password:

     

    Test it:

     

    Saturn:.ssh bb$ ssh dude@10.0.2.5 "uname -sr"

    Linux 3.8.13-16.2.1.el6uek.x86_64

     

    Voila, this time it did not ask for a password anymore! So you could then use ssh commands in cron to execute them on the remote host. The DSA key-pair will also work with SFTP and SCP, which are both part of SSH. The older RSA key is no longer required, only for some older licensed versions of SSH.

     

     

    How to use cron? There is lots off info on the web.

     

    Message was edited by: Dude! I thought I would point out that is probably nothing wrong using RSA instead of DSA. Unfortunately some of the information on the web is already outdated. RSA apparently supports so called Perfect Forward Secrecy as well as DSA, which generate random public keys per session for the purposes of key agreement that is not based on any sort of deterministic algorithm. Historically, version 1 of the SSH protocol supported only RSA keys. When version 2 was defined, RSA was still patented, and DSA was added to support an opensource patent-free implementation. RSA patent expired more than 10 years ago.

  • 3. Re: Run cronjob on remote server from local machine
    Tushar Lapani Newbie
    Currently Being Moderated

    Thanks

    I come to know that how i can copy a file by using ssh (to bypass password) while connecting with remote host from local host. But I am not getting your point that if I write .sh file on local machine (SERV1) then how i can run it on remote host(SERV2) ?

     

    Thanks & Regards

    Tushar Lapani

  • 4. Re: Run cronjob on remote server from local machine
    Dude! Guru
    Currently Being Moderated

    Copying the DSA public key to the remote host is part of the password less login setup. If you want to copy a file to a remote machine you would use SCP. I did not make a point of copying a file to a remote machine, but was demonstrating how you can use SSH to execute commands on a remote machine, for which password less login is necessary, especially if you wish automate the task.

     

    You can also execute a local script on a remote host using the previous setup, for example:

     

    Saturn:~ bb$ cat script

    ls -al

    echo $HOME

    who am i

     

    Saturn:~ bb$ ssh -T dude@10.0.2.5 < script

    total 28

    drwx------. 3 dude dude 4096 Dec  9 16:40 .

    drwxr-xr-x. 3 root root 4096 Dec  9 16:37 ..

    -rw-------. 1 dude dude    5 Dec  9 16:38 .bash_history

    -rw-r--r--. 1 dude dude   18 Jul 18 16:33 .bash_logout

    -rw-r--r--. 1 dude dude  176 Jul 18 16:33 .bash_profile

    -rw-r--r--. 1 dude dude  124 Jul 18 16:33 .bashrc

    drwx------. 2 dude dude 4096 Dec  9 16:40 .ssh

    /home/dude

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points