Results 1 to 6 of 6

Thread: Problem running script as $USER

  1. #1
    Join Date
    Nov 2011
    Beans
    13

    Problem running script as $USER

    Hi all,

    I have problem to run the following script as user ($USER).
    I have setup cronjob to run the script every 3 minutes.

    Basically, the script will check the running process for the user and start the process as the user if the process is not running.

    Code:
    #!/bin/sh
    PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/bin:/sbin
    SERVICE='irssi'
    HOMEDIR="/home/$USER"
    SIZE=95 #total %age
    
    
    df $HOMEDIR| tail -1 | while read fs size used avail pcnt mount;
    do
      pcnt=$(echo ${pcnt} | cut -d'%' -f1 )
      if [ ${pcnt} -ge $SIZE ]; then
        echo "Running out of space \"${fs} (${pcnt}%)\" on ${HOSTNAME} as on $(date)"
        exit 1
      fi
    
    
    if pgrep -u $USER $SERVICE > /dev/null
    then
        echo "$SERVICE service running, everything is fine"
    else
        echo "$SERVICE is not running, starting $SERVICE" && screen -d -m -S testscript $SERVICE
    fi
    done
    I name the script as chkirssi, chmod 777 the script.

    When I check the cronjob log, I can see there's an error as below:
    pgrep: invalid user name: irssi

    I have change the $USER to $LOGNAME, same error occur.

    I setup the cronjob for the user as below:

    sudo crontab -u sil3nthunt3r -e

    @reboot /usr/local/bin/chkirssit >/dev/null 2>&1
    @reboot /usr/bin/screen -D -m /usr/local/bin/rtorrent
    */3 * * * * /usr/local/bin/chkirssi >>/home/sil3nthunt3r/log.txt 2>&1


    Kindly need your input on this.

    Thanks

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,301
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Problem running script as $USER

    looks like $USER is null.
    Code:
    $ user=''
    $ pgrep -u $user irrsi
    pgrep: invalid user name: irrsi
    Btw, do yourself a favor and always quote your variables.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  3. #3
    Join Date
    Nov 2011
    Beans
    13

    Re: Problem running script as $USER

    Quote Originally Posted by Vaphell View Post
    looks like $USER is null.
    Code:
    $ user=''
    $ pgrep -u $user irrsi
    pgrep: invalid user name: irrsi
    Btw, do yourself a favor and always quote your variables.
    Hi,

    $USER variable suppose to grab the username of the user from the system correct?
    This script run in multiuser environment where each user have their own crontab that will run this script.
    That is why $USER is being use. But not sure why it did not work.

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,301
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Problem running script as $USER

    do a test - echo $USER and other important variables to your log.txt file to see if you work with what you think you work with.
    Last edited by Vaphell; September 30th, 2013 at 01:04 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  5. #5
    Join Date
    Sep 2006
    Beans
    7,298
    Distro
    Lubuntu Development Release

    cron environment

    For whatever reason, $USER is not one of the environment variables set in cron.
    $LOGNAME is set, however. It might be useful to have your script verify that one or the other of those variables is available before passing them on to other programs.

    You can see all of what is available to you in cron by adding a cronjob like this:

    Code:
    set > /tmp/sil3nthunt3r
    Then check the contents of that file after the cron job has run.

  6. #6
    Join Date
    May 2009
    Location
    Fareham, UK
    Beans
    Hidden!
    Distro
    Xubuntu 13.04 Raring Ringtail

    Re: Problem running script as $USER

    Crontabs only inherit a very basic environment with next to no preset variables, you can set $USER in your crontab if you want to specify it, just put it above your script entry because crontabs are evaluated in a linear fashion
    Catch me on Freenode - imark

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
  •