LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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


Reply
  Search this Thread
Old 04-21-2014, 09:09 PM   #1
anon112
Member
 
Registered: May 2008
Distribution: Arch, FreeBSD
Posts: 116

Rep: Reputation: 17
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.
 
Old 04-21-2014, 09:27 PM   #2
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
i don't know about systemd, but what comes to mind is having a cron job with a time field of @reboot.
 
Old 04-21-2014, 10:14 PM   #3
anon112
Member
 
Registered: May 2008
Distribution: Arch, FreeBSD
Posts: 116

Original Poster
Rep: Reputation: 17
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?
 
Old 04-21-2014, 10:23 PM   #4
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
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.
 
Old 04-21-2014, 11:11 PM   #5
anon112
Member
 
Registered: May 2008
Distribution: Arch, FreeBSD
Posts: 116

Original Poster
Rep: Reputation: 17
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).
 
Old 04-21-2014, 11:25 PM   #6
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
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.
 
Old 04-21-2014, 11:43 PM   #7
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
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.
 
Old 04-22-2014, 10:23 AM   #8
anon112
Member
 
Registered: May 2008
Distribution: Arch, FreeBSD
Posts: 116

Original Poster
Rep: Reputation: 17
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.
 
  


Reply

Tags
email, postfix, startup, user


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] sendmail - Solaris 10, can't send mail to a specific user on the system slufoot80 Solaris / OpenSolaris 3 01-14-2013 12:09 AM
Only receive the email from specific user in postfix unibox Linux - Server 3 12-04-2012 04:46 AM
postfix send specific email reject bartgymnast Linux - Server 0 09-10-2012 03:20 AM
Send email to specific SMTP servers per email from my server? neilius Linux - Server 6 06-25-2009 07:31 AM
how to restrict a user to send mails only to 3 email IDs in postfix mail server sharath41 Linux - Server 1 07-04-2008 04:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02: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