Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Do some commands on startup

  1. #1
    Join Date
    Jan 2008
    Beans
    62
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Thumbs up Do some commands on startup

    I'm not sure if Im in the correct place at these forum to ask this question, but I want to run this command at start up:

    Code:
    $ sdptool browse 00:16:B8:1A:C0:80
    Then i will get a respons like this (it's just a abridgement of the complete "answer"), IF my Cellphone has the Bluetooth turn on:
    Code:
    Service Name: Dial-up Networking
    Service RecHandle: 0x10002
    Service Class ID List:
      "Dialup Networking" (0x1103)
      "Generic Networking" (0x1201)
    Protocol Descriptor List:
      "L2CAP" (0x0100)
      "RFCOMM" (0x0003)
        Channel: 2
    Profile Descriptor List:
      "Dialup Networking" (0x1103)
        Version: 0x0100
    Then i wanna check what "Channel" is set to, then run this command (gedit og nano?):
    Code:
    $ sudo gedit /etc/bluetooth/rfcomm.conf
    The file rfcomm.conf will contain something like this:
    Code:
    #
    # RFCOMM configuration file.
    #
    
    rfcomm0 {
    	device 00:16:B8:1A:C0:80;
    	channel 2;
    	comment "BT DUN";
    }
    And then check if "channel" is set to (see line 7 in code-snippet above) the same as "Service Name: Dial-up Networking"-respons from my Cellphone. If not, the script should write to the file "rfcomm.conf", and change channel to the correct channel number.

    At the end of the script this command should be runned:
    Code:
    $ sudo rfcomm bind rfcomm0
    If the Cellphone does not respond, i will get this message in terminal "Failed to connect to SDP server on 00:16:B8:1A:C0:80: Host is down", but the code above should be runned anyway.

    Does anyone know how i could do this? I'm new to linux..
    Last edited by nukleuzN; January 7th, 2008 at 12:33 AM.

  2. #2
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Do some commands on startup

    I suggest you look into Perl since it seems that you need to do some text processing
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  3. #3
    Join Date
    Jan 2008
    Beans
    62
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Do some commands on startup

    Since I dont know Perl, i tried to do something i PHP.

    http://norskwebforum.no/pastebin/10269

    But first, i need to run this on my startup. And second, how am i able to run sudo-commands in shell_exec?
    This is probably not the best code that is written. But this is the first time i use Exceptions

    Thanx

  4. #4
    Join Date
    Jun 2007
    Location
    Tacoma, WA
    Beans
    244
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Do some commands on startup

    Is it correct that you want to run your script at system boot, as opposed to when you simply log in to a session?

    If so, consider creating an init.d startup file. Linux borrows the SvsV-Unix init process, and you can read more about it by running 'man update-rc.d'. Essentially, a series of scripts are executed in a specified order by an 'init' process. Ubuntu sets up the basic scripts and ordering for you, but you are able to modify this or add your own startup scripts.

    Try with a simple example script to verify the init process is doing what you think, and then add your desired components piece by piece.

  5. #5
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Do some commands on startup

    if you know php, perl is not far away (except that there are real arrays and hashes instead of associative arrays)

    but using php should suffice
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  6. #6
    Join Date
    Jan 2008
    Beans
    62
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Do some commands on startup

    But this script works fine to me, the only thing I'm missing are how to run Sudo-commands (root previleges) with shell_exec in php?

    And i think i will find out by my self how i will be able to start this by a Cron 1 minute after startup

    EDIT: The only thing i need is to run the "private function StartConnection ()" in the destuctor as root The script remake the configuration file when needed... (if channels are different)
    Last edited by nukleuzN; January 7th, 2008 at 07:37 AM.

  7. #7
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Do some commands on startup

    if you run your script at boot up, then it will run as root. (no need for sudo)
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  8. #8
    Join Date
    Jan 2008
    Beans
    62
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Do some commands on startup

    Hi again!

    I have now updatet my code with the shebang line, found the correct "shebang line" when typing "which php" in terminal after installing "php5-cli".

    After i have typed "crontab -e" in terminal, nano opens the file. And as I could see, I'm only able to run this with crontab at a designated time ?

    How can i add "php -q /var/www/sysdoc/config.startup.php" to boot up?

  9. #9
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Do some commands on startup

    here's one that does the parsing and editing
    Code:
    #!/bin/bash
    sdptool browse 00:16:B8:1A:C0:80 | awk  '/Channel:/ { 
     bluetoothchannel=$2
     while ( ( getline line < "/etc/bluetooth/rfcomm.conf" ) > 0 ) {
        if ( line ~ /channel/ && line !~ /#/ ) {
          split(line,s)
          sub(";","",s[2])
          if ( s[2] != bluetoothchannel  ){
             print "  channel " bluetoothchannel ";" > "temp"
          }
        }
        else {
          print line > "temp"
        }
     }
     close("/etc/bluetooth/rfcomm.conf")
     }
     /Failed to connect/ { print $0 }'
    I leave you to create the startup script. You can take references from the web by googling "How to create startup scripts ubuntu"

  10. #10
    Join Date
    Jan 2008
    Beans
    62
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Do some commands on startup

    Quote Originally Posted by slavik View Post
    if you run your script at boot up, then it will run as root. (no need for sudo)
    I did this:

    1: made a file named: bluetoothinit.sh
    3: with this content:
    #!/bin/bash
    php -q /var/www/sysdoc/config.startup.php
    2: saved it in /ect/rc6.d/

    restarted, and nothing happend

    Is there something wrong with the shell file?

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •