You are not logged in.

#1 2008-08-11 06:32:46

colbert
Member
Registered: 2007-12-16
Posts: 809

How to have script run in cron when it requires root password?

I am trying to place this script on my server so that it checks if my Mythbackend is running and restarts it if (I should say when, doggone thing lol) it crashes. Of course the command I use is sudo /etc/rc.d/mythbackend restart, and this user who gave me the script is not using Arch so I thought I'd ask the gurus here to help me figure out how to get it going.

First part is the script and then his cron instruction:

-bash-3.2$ cat ~root/cron/backend.sh
#! /bin/bash

if ps ax | grep -v grep | grep mythbackend > /dev/null
then
  echo "Mythbackend service ok" > /dev/null
else
  #echo "Mythbackend service is not running"
  echo "Mythbackend is not running"
  /etc/rc.d/mythbackend restart
fi



and in /etc/crontab I have:
#Check mythbackend is running
0-59 * * * *    root    /root/cron/backend.sh

Of course I see I have /etc/cron.daily, etc. so I'm unsure where to put it and how the cronjob will change on here, as well as of course how to get it running without needing me to enter the root password. TIA for any help smile

Offline

#2 2008-08-11 07:58:02

vacant
Member
From: downstairs
Registered: 2004-11-05
Posts: 816

Re: How to have script run in cron when it requires root password?

Here's my /etc/sudoers entry to allow me to run pacman:

paul night= NOPASSWD: /usr/bin/pacman

"man pidof" would probably help with a more pleasing result than that "ps/grep/grep" stuff.

As the user, "crontab -e" and insert the frequency/command to run, then "crontab -l" to check.

Last edited by vacant (2008-08-11 08:01:38)

Offline

#3 2008-08-11 17:12:11

colbert
Member
Registered: 2007-12-16
Posts: 809

Re: How to have script run in cron when it requires root password?

Okay, so for me would this be right:

bobby= NOPASSWD: /etc/rc.d/mythbackend

??

Offline

#4 2008-08-11 18:10:24

vacant
Member
From: downstairs
Registered: 2004-11-05
Posts: 816

Re: How to have script run in cron when it requires root password?

I think you may need your host name between user and "=" (my host name is night).

Offline

#5 2008-08-11 18:24:16

colbert
Member
Registered: 2007-12-16
Posts: 809

Re: How to have script run in cron when it requires root password?

Thanks! I have added this line:

bobby mythbox=NOPASSWD: /etc/rc.d/mythbackend

How does this "activate" do I need to log out and back in or?

Offline

#6 2008-08-11 21:10:50

chimeric
Member
From: Munich, Germany
Registered: 2007-10-07
Posts: 254
Website

Re: How to have script run in cron when it requires root password?

I guess you have a root account on the server so why not run it as root (with the system crontab)? You gain very much nothing here by using sudo. Also, just in case if this server is publicly accessible, I'd never setup passwordless sudo for a normal user account.

Offline

#7 2008-08-11 21:19:14

colbert
Member
Registered: 2007-12-16
Posts: 809

Re: How to have script run in cron when it requires root password?

Yes chimeric it is my home server, so how would I run it as root with system crontab? Sorry but I have never done this cron part before smile Thx for your help smile

Offline

#8 2008-08-11 21:23:18

Zepp
Member
From: Ontario, Canada
Registered: 2006-03-25
Posts: 334
Website

Re: How to have script run in cron when it requires root password?

colbert wrote:

Yes chimeric it is my home server, so how would I run it as root with system crontab? Sorry but I have never done this cron part before smile Thx for your help smile

same as user, just edit the root user's crontab instead. So bump yourself up to root first, and then run crontab -e

Offline

#9 2008-08-11 23:10:53

colbert
Member
Registered: 2007-12-16
Posts: 809

Re: How to have script run in cron when it requires root password?

Thanks Zepp! Okay so just to be clear I don't have to change /etc/sudoers to add that nopasswd line, but I can just have my script added to a cronjob with sudo crontab -e and it will run the command of the cronjob without me having to enter root passwd?

Last edited by colbert (2008-08-11 23:11:21)

Offline

Board footer

Powered by FluxBB