0

If I execute this script manually in Debian:

#!/bin/bash
/sbin/ifconfig wlan0 down
/sbin/ifconfig wlan0 up
sudo iwconfig wlan0 essid "WLAN_NETWORK"
sudo iwconfig wlan0 key myPassword
sudo dhclient wlan0

It works fine. It restarts my wifi adapter and reconnects again without problem. However, it does nothing when I program it with cron (Of course with administrator permissions).

Any hint of what is going on?

Janus Gowda
  • 103
  • 1
  • 5
  • 2
    \ is not the path separator on linux. / is. That cannot be the set of commands you are running by hand when you test it. – Etan Reisner Jul 30 '13 at 17:55
  • Copy and paste your scripts. Typing them in by hand will lead to errors that distract people from answering the actual question (and may mask other errors in your script). – larsks Jul 30 '13 at 17:57

1 Answers1

4

You appear to have some "useless use of sudo" in this script. That is, if you can run this successfully:

/sbin/ifconfig wlan0 down

Then you're probably already root, so you should be able to:

/sbin/iwconfig wlan0 essid "WLA_NETWORK"

without using sudo.

There are a couple of things that could prevent your script in its current form from running:

  • sudo can be configured to require a valid tty (using the requiretty configuration directive). If this flag is active you won't able to use sudo via cron.
  • You're using fully qualified paths for ifconfig but not for iwconfig. If this command isn't in the PATH available when run via sudo, it's not going to work.

Here are somethings you can do to fix it:

  • Remove the use of sudo. Either run the entire script via sudo or just run it as root.
  • Replace iwconfig and dhclient with fully qualified paths.
  • Log stdout and stderr from the script to a file so that you can inspect the output. Your crontab entry would look something like * * * * * /path/to/your/script > /tmp/script.log 2>&1.

If your script is still having problems at this point, any errors logged in the output file show help point the way to a solution.

larsks
  • 43,623
  • 14
  • 121
  • 180