Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.
#!/bin/bash
value=$(<man.txt)
echo "$value"

if [ "$value" == "true" ]; then
    echo "startedif_manthan"
      ps -ef|grep sym |awk '{ print $2 }'|sudo  xargs kill -9;
      sleep 30;
    sudo sh /var/www/symmetric-ds-3.1.6/bin/sym --port 8082 --server;
fi

I need to run above script in cron. Will it execute in cron without password?

Any suggestions?

share|improve this question
    
It will only execute without needing a password prompt if you are running it from root's crontab or if you have specifically given these password-less rights to the user in the sudo configuration file, editable through the command visudo. –  Daniel Andersson Feb 14 '13 at 7:51
    
A duplicate and another duplicate –  Sithsu Jan 20 at 21:40

2 Answers 2

up vote 0 down vote accepted

You can configure sudo not to require a password:

  1. Make /var/www/symmetric-ds-3.1.6/bin/sym executable:

    $ chmod a+x /var/www/symmetric-ds-3.1.6/bin/sym
    
  2. Replace in your script:

    sudo sh /var/www/symmetric-ds-3.1.6/bin/sym --port 8082 --server
    

    with:

    sudo /var/www/symmetric-ds-3.1.6/bin/sym --port 8082 --server
    
  3. Use visudo to add the following line to /etc/sudoers:

    <user who runs the cronjob> ALL = NOPASSWD:/path/to/xargs [-0-9 a-z]*, /var/www/symmetric-ds-3.1.6/bin/sym [-0-9 a-z]*
    
share|improve this answer
    
The user will also need to give password-less rights to xargs to kill the program as the script stands. –  Daniel Andersson Feb 14 '13 at 8:06
    
Thanks for effort guys. Appreciate your help. –  user1597811 Feb 14 '13 at 9:39

I'd rather lay out the script similar to:

#!/bin/sh
read value < /path/to/man.txt

if [ "$value" = "true" ]; then
    pkill -u root '^sym$' && sleep 30
    sh /var/www/symmetric-ds-3.1.6/bin/sym --port 8082 --server
fi

Since you want to run every command as root anyway, put it in root's crontab. If you don't need to run anything as root, don't. If you actually need to run it via sudo through specifically configured rights for certain applications, bring back sudo, but note that you also will need specifically configured rights to kill the program (unless it has an equivalent to a --kill switch).

I stripped the echos since unnecessary print statements in a crontab will just clutter logs. You could implement a logging mechanism within the script if its necessary.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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