LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 06-18-2014, 03:45 AM   #1
kunalks
Member
 
Registered: Dec 2013
Posts: 53

Rep: Reputation: Disabled
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.
 
Old 06-18-2014, 03:57 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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)
 
Old 06-18-2014, 04:07 AM   #3
kunalks
Member
 
Registered: Dec 2013
Posts: 53

Original Poster
Rep: Reputation: Disabled
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?
 
Old 06-18-2014, 04:09 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
see man crontab. as root you can edit other users' cronjobs too.
crontab -u user -e
 
Old 06-18-2014, 08:17 AM   #5
Rawcous
Member
 
Registered: Jan 2014
Location: Farnborough, Hampshire - UK
Distribution: SCO UNIX -> Fedora (Core) -> CentOS -> RedHat
Posts: 128

Rep: Reputation: 48
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
 
Old 06-19-2014, 06:11 AM   #6
kunalks
Member
 
Registered: Dec 2013
Posts: 53

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Rawcous View Post
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
 
Old 06-19-2014, 06:34 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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
}
 
Old 06-19-2014, 11:18 PM   #8
Tadaen
Member
 
Registered: Sep 2005
Distribution: Arch
Posts: 210

Rep: Reputation: 39
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.

Last edited by Tadaen; 06-19-2014 at 11:27 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Debian daily cron job won't run, but does run in cron.hourly. sandersch Linux - General 7 05-24-2012 01:50 AM
[SOLVED] Not able to run cron job for non-privileged user in RHEL 5.3 mail4vijay Linux - General 3 11-24-2009 10:18 AM
How to create user account with a 'newusers' run from a cron job? tifoso Linux - General 3 11-05-2009 06:19 AM
cron job as a non-privileged user do not run chakkerz Linux - General 5 06-03-2008 08:28 PM
Can't get a cron job to run derzok Linux - General 10 12-16-2007 04:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 09:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration