Results 1 to 8 of 8

Thread: start/stop ssh from crontab

  1. #1
    Join Date
    Jul 2012
    Beans
    3

    start/stop ssh from crontab

    Hi, I'm using 12.04 LTS server, and i want to turn on/off ssh in a crontab in order to open a small window of time for my backup (via rsync/ssh) to connect.

    I figured it'd be as easy as putting

    service ssh start
    service ssh stop

    into the appropriate root crontabs (using "crontab -e" as root), but doing that creates an email from the crontab daemon that says:

    /usr/bin/service: 123: exec: start: not found

    i tried "/etc/init.d/ssh start" (/stop) as well, same error

    So then i tried creating my own bash script to properly initialize the shell:

    ----startssh.sh-----
    #!/bin/bash
    source /etc/profile
    service ssh start
    --------------------

    and it, again, outputs the same error to an email:

    /usr/bin/service: 123: exec: start: not found

    what am I doing wrong?? How can i start/stop ssh at certain times "the proper way" i know I could just run "/usr/sbin/sshd -D" myself, but thought it was better to use the proper startup/shutdown scripts...

    Thanks!
    James

  2. #2
    Join Date
    Apr 2008
    Location
    LOCATION=/dev/random
    Beans
    5,767
    Distro
    Ubuntu Development Release

    Re: start/stop ssh from crontab

    Entries in crontab don't have a full environment set up. Try using:
    Code:
    /usr/sbin/service ssh start
    and
    Code:
    /usr/sbin/service ssh stop
    Cheesemill

  3. #3
    Join Date
    Jul 2012
    Beans
    3

    Re: start/stop ssh from crontab

    Quote Originally Posted by Cheesemill View Post
    Entries in crontab don't have a full environment set up. Try using:
    Code:
    /usr/sbin/service ssh start
    and
    Code:
    /usr/sbin/service ssh stop
    Same thing, /usr/bin/service is simply a symlink to /usr/sbin/service

    i know its running the service script, but its soemthing in the service script that is failing (like the start or exec) because it doesnt have the appropriate environment initialized... i thought thats what doing "source /etc/profile" would do?

    /usr/sbin/service: 123: exec: start: not found

  4. #4
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    6,590
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: start/stop ssh from crontab

    Hi lightbox.

    This is a tricky one.

    'crontab' has a limited environment, included a very short path. Although 'service' is on this very basic path, 'service' is actually an script that that calls other scripts.

    start, stop and status are not real parameters, but other programs that live on /sbin, which is not in the limited crontab path.

    You can check it yourself by doing:
    Code:
    $ ls /sbin/stop /sbin/start /sbin/status 
    /sbin/start  /sbin/status  /sbin/stop
    The easy solution then, is to expand the path as the first line of your crontab, so that includes /sbin. For instance:
    Code:
    # m h  dom mon dow   command
    PATH=/usr/sbin:/usr/bin:/sbin:/bin
    */1 * * * * service mysql stop
    I hope that helps, and let us know how it goes.
    Regards.

  5. #5
    Join Date
    Apr 2012
    Beans
    5,057

    Re: start/stop ssh from crontab

    this is a bug I think -

    https://bugs.launchpad.net/ubuntu/+s...t/+bug/1008892

    try setting PATH="/sbin:$PATH" explicitly

    EDIT: oops papibe beat me to it
    Last edited by steeldriver; July 11th, 2012 at 06:40 PM.

  6. #6
    Join Date
    Jan 2009
    Location
    ::1
    Beans
    2,460

    Re: start/stop ssh from crontab

    Quote Originally Posted by lightbox View Post
    ...

    /usr/bin/service: 123: exec: start: not found

    what am I doing wrong?? How can i start/stop ssh at certain times "the proper way" i know I could just run "/usr/sbin/sshd -D" myself, but thought it was better to use the proper startup/shutdown scripts...

    Thanks!
    James
    Have you tried the old-skool

    Code:
    /etc/init.d/ssh stop
    in your root's crontab?

  7. #7
    Join Date
    Jul 2012
    Beans
    3

    Re: start/stop ssh from crontab

    Quote Originally Posted by papibe View Post
    Hi lightbox.

    This is a tricky one.

    'crontab' has a limited environment, included a very short path. Although 'service' is on this very basic path, 'service' is actually an script that that calls other scripts.

    start, stop and status are not real parameters, but other programs that live on /sbin, which is not in the limited crontab path.

    You can check it yourself by doing:
    Code:
    $ ls /sbin/stop /sbin/start /sbin/status 
    /sbin/start  /sbin/status  /sbin/stop
    The easy solution then, is to expand the path as the first line of your crontab, so that includes /sbin. For instance:
    Code:
    # m h  dom mon dow   command
    PATH=/usr/sbin:/usr/bin:/sbin:/bin
    */1 * * * * service mysql stop
    I hope that helps, and let us know how it goes.
    Regards.
    Yup that did it! Thank you so much:

    Code:
    PATH=/usr/sbin:/usr/bin:/sbin:/bin
    4   4 * * *     service ssh start
    30  4 * * *     service ssh stop
    James

  8. #8
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    6,590
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: start/stop ssh from crontab

    Great!

    Please mark the thread solved (read here) when you have the chance.

    Regards.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •