Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
04-21-2014, 09:09 PM
|
#1
|
Member
Registered: May 2008
Distribution: Arch, FreeBSD
Posts: 116
Rep:
|
postfix: how do I send an email at system startup from a specific user?
I'm trying to set up a system whereby every time my computer starts, systemd executes a program to send an email to me detailing my computer's IP address and approximate location. I have written a script (below) which gathers the information I need and sends an email, however I need systemd to run this software as my computer's main user instead of root.
So, say for example my main user is "user". I want to write a service started by systemd on startup that runs a program as "user" to send an email as that user. Perhaps there's a better way to do this with postfix, I'm still new to it. Does anybody know how I could accomplish this? Any suggestions for improvement are welcome.
The script:
Code:
COMPUTER="ArchLinux Main System" #Set this to whichever machine this is installed on.
Cont=0;
while [ $Cont == 0 ]; do
sleep 20
ping -c 1 -q 208.67.222.222;
NetStat=$?;
if [ $NetStat == 0 ]; then
IP=`curl http://ipecho.net/plain; echo`
NetLoc=`curl ipinfo.io/$IP; echo` #Not very accurate for location, but good enough.
Cont=1
MESSAGE="The following inormation is related to $COMPUTER: \nIP Address: $IP\nNetwork Address Information: $NetLoc";
echo -e "$MESSAGE" | mailx -s "Network Information for $COMPUTER" <user@batman.penguin> #INSERT EMAIL HERE
fi
done
Currently when I run this with a simple systemd script through ExecStart, it will run it as root and tends to never send the email.
|
|
|
04-21-2014, 09:27 PM
|
#2
|
Senior Member
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625
Rep: 
|
i don't know about systemd, but what comes to mind is having a cron job with a time field of @reboot.
|
|
|
04-21-2014, 10:14 PM
|
#3
|
Member
Registered: May 2008
Distribution: Arch, FreeBSD
Posts: 116
Original Poster
Rep:
|
Sadly, I've had troubles with cron in the past. I'd much prefer not to, but if there's no alternative, I guess I'll have to. Would that run as root, or would it execute as my main user?
|
|
|
04-21-2014, 10:23 PM
|
#4
|
Senior Member
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625
Rep: 
|
Quote:
Would that run as root, or would it execute as my main user?
|
the script would run as an unprivileged user, not as root. you can install it in the user's crontab or in the system crontab (/etc/crontab). note that the system crontab requires an extra field, which is the user executing the command.
|
|
|
04-21-2014, 11:11 PM
|
#5
|
Member
Registered: May 2008
Distribution: Arch, FreeBSD
Posts: 116
Original Poster
Rep:
|
if I need to install it in the user's crontab, then wouldn't that require the user to have logged in first? The point of this is to execute a command as the user before the user formally logs in (in this case, send an email as "user" when the computer starts, but before the user has entered their password and logged in).
|
|
|
04-21-2014, 11:25 PM
|
#6
|
Senior Member
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625
Rep: 
|
Code:
if I need to install it in the user's crontab, then wouldn't that require the user to have logged in first?
no.
|
|
|
04-21-2014, 11:43 PM
|
#7
|
Senior Member
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625
Rep: 
|
Something about cron, though, is that it has a limited set of environment variables, and the values may be different from what they are when you log in. In particular, PATH is narrow, so it may not find all executables you have in your script. One thing you can do is set PATH in your script (e.g. you can set it to whatever it is in your use's shell). Alternatively, you can specify the full path to filenames in your script (e.g. /usr/sbin/mailx instead of mailx). You'll need to test your script. First, make sure it works on the command line, then give it a test run through cron. Any output (stdout and stderr) will be emailed to the user.
|
|
|
04-22-2014, 10:23 AM
|
#8
|
Member
Registered: May 2008
Distribution: Arch, FreeBSD
Posts: 116
Original Poster
Rep:
|
Yeah, cron still doesn't work very well on my computer... Fortunately, through searching around on systemd stuff, I found a missing field in my service script that would have really simplified things from the start.
Under the [Service] section, I had to add an entry for User: User=<user>
Now I have this service set to run at startup and I can use systemd to check that the required services are in fact running.
Thanks anyway for your help.
|
|
|
All times are GMT -5. The time now is 02:03 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|