Tell me more ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

My question may seem weird but it really makes sense if your internet connection keeps going off every few hours.

I want to know how I can restart the network-manager by using a shell script.

now i know that sudo service network-manager restart is how you do it in the terminal, but sadly that doesn't work on a shell script.

Any ideas ?

share|improve this question

3 Answers

up vote 2 down vote accepted

No its not weird at all, I too experience connectivity issues a lot when I use my usb net-setter modem.

enter image description here

Here's how you do it

gksu service network-manager restart

save it in a file with .sh extension and grant executable file permission by right clicking>>properties>>permissions

share|improve this answer
Thanks, it works like a breeze! – Rupali May 25 '12 at 14:26
@virpara Have I mentioned anywhere that it wouldn't work without the extension ? – Srinivas Gowda May 25 '12 at 14:44
@virpara: Sorry but I'm not on a GSM network.. – Rupali May 25 '12 at 15:05
@Rupali which network you are on? script can be modified according to that. – virpara May 25 '12 at 15:07
@virpara: thats very sweet of you, but Srinivas Gowda's script is simple and it does the job really quick and easy. – Rupali May 25 '12 at 15:10
show 1 more comment

I too had a very similar problem. My internet is so flaky that when there are power fluctuations the modem goes off line and can't be seen unless you pull the modem and plug it in again. Otherwise you can use usb_modeswitch. So I've taken the above script and tweaked it to do both tasks.

#!/bin/bash

while true; do
#Anything less than a solid connection reboots the USB modem
LC_ALL=C nmcli -t -f TYPE,STATE dev | grep -q "^cdma:connected$"
if [ ! $? -eq 0 ]; then

#Reset USB Modem (12d1:1001 will have to be changed to match your modem)
sudo usb_modeswitch -R -v 12d1 -p 1001
#Wait 20 Seconds before trying to bring up the Broadband connections
sleep 20
    nmcli -t nm wwan on
#Wait Another 20 Seconds then test if the connection came up on its own as it is set to auto-connect
    sleep 20
LC_ALL=C nmcli -t -f TYPE,STATE dev | grep -q "^cdma:disconnected$"
if [ $? -eq 0 ]; then        
    nmcli -t con up id "Zantel connection"
    sleep 15
fi
    #wait approximately 15 sec to get connected

fi
#it does not worth keep it checking every millisecond.
#my connection will be reestablished within 5-15 seconds
sleep 2
#if anyone can code it better please feel free to comment
#TO-DO:: check for data received. if data < 15 KB after 20 seconds of connection
#reconnect mobile broadband connection  
done

Thanks!

share|improve this answer

nmcli is a command-line tool for controlling NetworkManager and getting its status.

I also had same problem while using Mobile Broadband.

I have created a shell script as follows. Save it, give execution permission and Put that in Startup Applications and it works like a charm ! It will connect automatically if connection is dropped. That is what I wanted.

You need to change network id (for me it is "Tata Docomo Internet"). Replace "Tata Docomo Internet" with name of your Mobile Broadband connection name.

#!/bin/bash

while true; do
    LC_ALL=C nmcli -t -f TYPE,STATE dev | grep -q "^gsm:disconnected$"
    if [ $? -eq 0 ]; then
        #jdownloader is still in the download status so stop it because
        #internet is disconnected and jdownloader won't resume download 
        #when connected again
        #jdownloader --stop-download
        #sometimes I can not get connected after disconnection when 
        #I click on <name of the network connection>. I have to disable
        #and enable Mobile Broadband
        nmcli -t nm wwan off
        sleep 1
        nmcli -t nm wwan on
        sleep 1
        nmcli -t con up id "Tata Docomo Internet"
        #wait approximately 15 sec to get connected
        #if anyone can add better command to check for it just comment it :-p 
        sleep 15
        #now connected to internet so start download
        #jdownloader --start-download
    fi
    #it does not worth keep it checking every millisecond.
    #my connection will be reestablished within 5-15 seconds
    sleep 2
    #if anyone can code it better please feel free to comment
    #TO-DO:: check for data received. if data < 15 KB after 20 seconds of connection
    #reconnect mobile broadband connection  
done
share|improve this answer
why this is down voted? – virpara May 25 '12 at 14:33
I voted you back up. People should not be allowed to down vote without a reason/comment. Nice script btw. =) – wojox May 25 '12 at 14:38
@wojox I had also problem with connection dropping. So searched on google and didn't find any solution. But now I don't know even when my connection disconnected and reconnected!!. BTW thanks ;-] – virpara May 25 '12 at 14:46
@virpara, I'm very interested in your approach because it doesn't require intervention. What do you mean by "which network you are on"? I have a cable connection which I've called DSL Connection 1. I had asked about exactly this problem some months ago here. If you would be kind enough to take a look, I'd appreciate it! – vasa1 May 25 '12 at 16:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.