LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-10-2020, 09:48 AM   #1
Rob0311
LQ Newbie
 
Registered: Apr 2020
Location: Switzerland
Distribution: Linux Mint
Posts: 4

Rep: Reputation: Disabled
[Solved] Executing a command when the network gets up


Hello everyone,

I am a retired engineer who tries to move all my systems from Windows 7 to Linux (mint in most cases), and already done for a laptop and a CNC controller.

I now replaced the Windows 7 of my desktop/server with Linux mint as well, running kernel 5.6.0

On my Windows system I used a 40" UHD TV screen via a HDMI port. The TV does not turn on when I switch my system on, but it responds to a WOL signal. On my Windows system I made a simple system service that sends the wakeup message via my LAN. I am now trying to replicate that in Linux.

I found the etherwake utility, and sending 'sudo etherwake -i enp5s0f1 70:54:b4:44:bc:ca' from my account works perfectly.

Since it is hard to login with a completely black screen, the WOL message needs to be sent as soon as the network is up. I am new to Linux, and this is where I have some problems, as I am not very familiar (yet) with the command language.

In /etc/network/ I found the directories if-up.d, if-down.d, if-pre-up.d etc) and I assume that systemd runs those contents when needed. Is that correct?

Above that directory sits a file with the following content:

# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback

From the man pages this looks like a loop, and I assume that systemd starts this loop when required?

The man pages for IFACE also specify a post-up 'command'

Would adding this to the above file run a loop until the interface is up for instance:

auto enp5s0f1
iface enp5s0f1
post-up /etc/network/<some_directory>/etherwake -i enp5s0f1 70:54:b4:44:bc:ca

That actually does not work. What would be the right approach The system has two Ethernet adapters, the enp5s0f1 is only used for the TV.

(Another issue is if the network is shut down during sleep, or if I need to call etherwake from another event as well. But that is a next step)

Thanks for help,
Rob

Last edited by Rob0311; 04-11-2020 at 08:09 AM. Reason: adding 'Solved'
 
Old 04-10-2020, 10:48 AM   #2
agillator
Member
 
Registered: Aug 2016
Distribution: Mint 19.1
Posts: 419

Rep: Reputation: Disabled
Using mint you need to deal with systemd. I know, I just made your day, right?

In effect you need to create a service to run once after the network is up. I had to do something similar recently and bludgeoned it into working so I'll show you what I did that seems to work. Someone who actually knows systemd may tell you something else. If so you probably need to listen to them but this did work for me.

My problem was getting some rules loaded in the firewall. I wrote a script to load them, and then had the script run once after the network was up. You will need to set the Description to something that makes sense for you and the ExecStart to the command line for you script. Be careful to use complete paths; do not expect the system to find the script without a complete path. The $PATH the computer uses will not be what you expect, if anything.

Put the service file in /etc/systemd/system and set the permissions to 644 with root as the owner. Then "sudo systemctl daemon-reload" and "sudo systemctl enable <service name>". That should do it, or at least come close.
Code:
[Unit]
Description=xxxxxxxxxxxx
After=network-online
[Service]
Type=oneshot
ExecStart=xxx/xxx/xxx
[Install]
WantedBy=network-online.target
Edit: I forgot to point out that the filename needs to be <something>.service

Last edited by agillator; 04-10-2020 at 12:41 PM. Reason: Added filename
 
2 members found this post helpful.
Old 04-10-2020, 10:59 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
There are a few ways to accomplish sending the command. In addition to the above you can use a system cron job. It isn't exactly what you asked for because the system will be fully running when the command is sent but is another option.

In the /etc/crontab file add the following line

@reboot root /path/to/etherwake -i enp5s0f1 70:54:b4:44:bc:ca

Where /path/to/ is the actual path to the etherwake utility. If you do not know try running the command

which etherwake

No need for sudo since the command will run as root.

Last edited by michaelk; 04-10-2020 at 11:03 AM.
 
2 members found this post helpful.
Old 04-10-2020, 02:59 PM   #4
Rob0311
LQ Newbie
 
Registered: Apr 2020
Location: Switzerland
Distribution: Linux Mint
Posts: 4

Original Poster
Rep: Reputation: Disabled
First attempt...

Thanks for the information. I created a file called wakeup.service located in /etc/systemd/system with chmod 644:

[Unit]
Description=WOL message to monitor
After=network-online
[Service]
Type=oneshot
ExecStart=/usr/sbin/etherwake -i enp5s0f1 70:54:b4:44:bc:ca
[Install]
WantedBy=network-online.target

Then:
sudo systemctl daemon-reload
sudo systemctl enable wakeup

Got a message that the service was created

Rebooted, but did not automatically turn on the monitor.
When the system came up, the network was dead.
Deleted the file wakeup.service and rebooted, network came back.
Looked in the syslog file, found the following message:

systemd[1]: /etc/systemd/system/wakeup.service:3: Failed to add dependency on network-online, ignoring: Invalid argument

Should I write a script instead of calling etherwake directly?


Rob

Will try the alternate method tomorrow. Thanks for the detailed comments.
 
Old 04-11-2020, 08:07 AM   #5
Rob0311
LQ Newbie
 
Registered: Apr 2020
Location: Switzerland
Distribution: Linux Mint
Posts: 4

Original Poster
Rep: Reputation: Disabled
solution found

With some more reading, I think I have found the solution. Systemd is actually a nice modular concept.

Waiting for network.target is no guarantee that the network is up; systemctl told me that the link was down in every test.
A good explanation of network status is in https://www.freedesktop.org/wiki/Sof...NetworkTarget/

Network-online.target functions as described, the following service works, with the script just calling etherwake with its parameters:
-------------------
[Unit]
Description=Wakeup the TV monitor
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
ExecStart=/etc/systemd/system/wakeup-mon.sh

[Install]
WantedBy=default.target


Thanks for the help provided. Now I need to find out were to hook the call when the system is coming out of sleep.

Rob
 
  


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
Crontab not executing my shell script its executing standlaone atulkvtiwari Linux - Newbie 15 12-04-2018 06:25 AM
Problems executing perl script with inputs when executing under other perl scripts TheStr3ak5 Programming 4 04-22-2017 04:07 AM
Executing Command on Remote Windows System from a Local Windows Command Prompt devUnix General 8 05-30-2012 07:51 AM
Executing shell command from JSP file with command line arg from URL orcusomega Programming 2 01-13-2012 03:38 PM
Bash Command Line Editor, while typing run another command before executing current? gumaheru Linux - General 5 04-13-2010 11:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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