smor
smor

Reputation: 39

Script to start network connection when disconnected

Here is a command I run to connect from my Ubuntu Linux server to a windows share folder:

sudo mount -t cifs //ipaddress/xml /var/www/dir/pub/xml -o user=username,password=password

The windows server restarts when updates are applied but I wouldn't know when it is restart so I would like to create a bash script on my Ubuntu Linux server to detect the disconnection and re-establish the connection...

for starters I would go about this:

#/bin/bash

if[/var/www/dir/pub/xml/* == '']; then
    sudo mount -t cifs //ipaddress/xml /var/www/dir/pub/xml -o user=username,password=password
fi

I would add this to a cron job to schedule it to run at least five minutes... I am not an expert at bash scripts but I would appreciate someone pointing me in the right direction... Thanks.

Upvotes: 2

Views: 492

Answers (1)

asimovwasright
asimovwasright

Reputation: 838

I would say you are on the right track, except I would change the script just a little:

#/bin/bash

if [ "$(ls -1 /var/www/dir/pub/xml/* | wc -l)" = 0 ] ; then
    mount -t cifs //ipaddress/xml /var/www/dir/pub/xml -o user=username,password=password
fi

exit

I changed the if statement, so it checks for content in the xml folder, and took sudo away from the mount command, since this will cause a password request on execution. Now you place the script into the:

sudo crontab -e

file...

Upvotes: 1

Related Questions