LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Run cron job with different User (https://www.linuxquestions.org/questions/linux-server-73/run-cron-job-with-different-user-4175508346/)

kunalks 06-18-2014 03:45 AM

Run cron job with different User
 
Hallo Guys ,
Cron job is also throwing now sendmail command.i just put that whole path of sendmail in my script /usr/sbin/sendmail and then it works.

can someone help me to find it out how can i run my cron job with different user measn now i am getting emails from root and i want that i create user called: Diskspace and send me this email.

Basically i want to run my cron job with Diskspace user so i get email from not root but from diskspace.
here is my cron job command.----> 0 0-23/4 * * * (. /etc/diskspace/vpndrive.sh) now i am running with root.

pan64 06-18-2014 03:57 AM

every user has its own crontab. You can try to use su to change user before executing that script. I do not know how do you send that mail, but you can also try to fake the sender (if it suits your needs)

kunalks 06-18-2014 04:07 AM

Hallo ,

i have script which check drive space ..



a=$(df -h /mnt/smb | tail -n +3 | tr -s ' ' | tr '%' ' ' | cut -d ' ' -f 5)
echo $a

if [[ $a -ge 30 ]]; then
cat /etc/diskspace/text.txt | /usr/bin/sendmail Test@gmail.com
fi


and now i have set this cron job with root user. i just want to set that other user run this cron job.

Can you please detail me little bit that how can i set this for another user. or how to access cron job for other user.

I need to give some rights to other user also to run sendmail command?

pan64 06-18-2014 04:09 AM

see man crontab. as root you can edit other users' cronjobs too.
crontab -u user -e

Rawcous 06-18-2014 08:17 AM

Hello,

As Pan64 mentions,

Quote:

see man crontab. as root you can edit other users' cronjobs too.
crontab -u user -e
You could also "fake" or if you prefer the term mask the sender's name by adding -f PseudoUsername to your sendmail statement. i.e.

Code:

a=$(df -h /mnt/smb | tail -n +3 | tr -s ' ' | tr '%' ' ' | cut -d ' ' -f 5)
 echo $a

 if [[ $a -ge 30 ]]; then
 cat /etc/diskspace/text.txt | /usr/bin/sendmail -f PsuedoUsername Test@gmail.com
 fi

where
Quote:

-f PseudoUsername
will cause the email to "appear" as if it's from the "fake" user so simply substitute PseudoUsername with the username you wish to emulate - in your case Diskspace (inspection of the email header via the mail client will reveal the true sender's identity - typically root@localhost) - I use this method myself for a number of different types of system emails.

Regards,

Rawcous

kunalks 06-19-2014 06:11 AM

Quote:

Originally Posted by Rawcous (Post 5189882)
Hello,

As Pan64 mentions,



You could also "fake" or if you prefer the term mask the sender's name by adding -f PseudoUsername to your sendmail statement. i.e.

Code:

a=$(df -h /mnt/smb | tail -n +3 | tr -s ' ' | tr '%' ' ' | cut -d ' ' -f 5)
 echo $a

 if [[ $a -ge 30 ]]; then
 cat /etc/diskspace/text.txt | /usr/bin/sendmail -f PsuedoUsername Test@gmail.com
 fi



where will cause the email to "appear" as if it's from the "fake" user so simply substitute PseudoUsername with the username you wish to emulate - in your case Diskspace (inspection of the email header via the mail client will reveal the true sender's identity - typically root@localhost) - I use this method myself for a number of different types of system emails.

Regards,

Rawcous


Thanks alot man !
it worked fine

pan64 06-19-2014 06:34 AM

just a comment: the first line will fork 6 processes, that is not a really optimal solution:
Code:

df -m /mnt/smb | awk '/smb/ { if ( $3/$2 > 0.3 ) exit 1 }' || {
 cat /etc/diskspace/text.txt | /usr/bin/sendmail -f PsuedoUsername Test@gmail.com
}


Tadaen 06-19-2014 11:18 PM

Code:

#!/bin/bash

as_user() {
        if [ $USER == $USERNAME ] ; then
                bash -c "$1"
        else
                su - $USERNAME -c "$1"
        fi
 }

This work? Found in a minecraft server script here : http://minecraft.gamepedia.com/Tutor...startup_script
Put the other part in another function then call that function from this one.


All times are GMT -5. The time now is 11:54 PM.