1

I wrote a simple shell script to shutdown an ubuntu server. Then I created a cron job to execute this script at 23:59 everyday.

#!/bin/bash
sudo shutdown -h now

I got this error message:

sudo: no tty present and no askpass program specified

when I removed the sudo from the code, I got a different error message:

/home/qinking126/scripts/test.sh: line 3: shutdown: command not found

How do I resolve this?

4

Make sure the cron script is run as root, and replace shutdown by its full path given to you by:

command -v shutdown

On Ubuntu GNU/Linux (and GNU/Linux in general), it should be /sbin/shutdown :

#!/bin/bash
/sbin/shutdown -h now

Note that adding a file named like /etc/cron.d/midnight-shutdown and containing:

59 23   * * *   root    test -x /sbin/shutdown && /sbin/shutdown -h now

should do what you want without the overhead of a full bash script. Here is the code to install this quickly:

echo '59 23 * * *   root    test -x /sbin/shutdown && /sbin/shutdown -h now' \
    | sudo tee '/etc/cron.d/midnight-shutdow'
sudo service cron reload

EDIT: To reply to your comment, another possibility of setting is:

echo '59 23 * * *   test -x /sbin/shutdown && /sbin/shutdown -h now' \
    | sudo tee '/etc/cron.d/midnight-shutdow'
sudo service cron reload

It is quite strange that cron print you this error message.

| improve this answer | |
  • got this error message "/bin/sh: 1: root: not found" – qinking126 Feb 4 '14 at 11:55
  • added another setting possibility. – Biapy Feb 4 '14 at 13:28
0

We can also use the command "sudo halt" to power off the Ubuntu Server.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.