How to launch a script on startup?

Discussion in 'Mac OS X System & Mac Software' started by Zeus, Feb 9, 2002.

  1. Zeus

    Zeus Registered

    Joined:
    May 11, 2001
    Messages:
    243
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Italy
    I need to launch a script on startup after autodiskmount execution.
    (the /etc/rc file has been already processed i suppose)
    What to do ???

    Is there a file to edit ??
     
  2. Zeus

    Zeus Registered

    Joined:
    May 11, 2001
    Messages:
    243
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Italy
    in Italy we say: gagliardo!!!
    it sounds like: Very very very cool!!!

    Thanks ...
    eventually i'll post some help request here!!
     
  3. jcihocki

    jcihocki Registered

    Joined:
    Feb 2, 2002
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    MAKE SURE your script backgrounds whatever it starts up. OSX is not very smart in regards to things like this yet, and it WILL NOT fork things it starts up automatically... and I've got news for you, this is a very painful thing to forget. If you put a script in StartupItems that doesn't background, Mac OSX startup will sit and wait for it to return. This means you can't go and change your startup disk to os9, and because when you start up command-s, you get a read only file system (real useful, eh?) So your only option is to start up from an OS 9 cd. (Good luck finding it, if you're like me). I searched around for a way to disable startup items on startup, but couldn't find one, and i'm not sure if one exists.

    That being said, have fun and be careful.
    -John
     
  4. blb

    blb `'

    Joined:
    Apr 9, 2001
    Messages:
    651
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    CO, USA
    Note that you can remount your disk in single-user to make it writable; simply run /sbin/mount -uw / to do this (it should also print this out when you boot single-user).
     
  5. Aqualung

    Aqualung Banned

    Joined:
    Nov 6, 2001
    Messages:
    45
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Ann Arbor
    As an alternative to the startup items, you could open the login preference panel and add the script to login items startup list. That way it runs whenever you log in.
     
  6. Zeus

    Zeus Registered

    Joined:
    May 11, 2001
    Messages:
    243
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Italy
    Excuse me if I'm annoying you ... it's because i'm new to Unix!

    I've some questions for who want to help me ...

    1)
    why i must add in the top of the script this line: #!/bin/sh?
    Isn't /bin/sh the sh shell paht ?? and if this line has the # symbol isn't this mean the command will not be executed ??
    ------------

    2)
    what about the 2nd line ?? . /etc/rc.common?? i've noticed this is used in the /etc/rc file too ! What's this command for ??
    ------------

    3)
    in the script is it possible to define a variable containing anothe variable?

    Example:
    variableone= /Volumes/MacOS9
    variabletwo=${variabletwo}/System Folder
    -----------

    4)
    how to tell the script that 'if a directory not exits' do 'create a directory' then (meaning the directory exist) 'continue script execution'
    -----------

    5)
    how to tell the script that 'if a file exist' do 'remove the the file' then (meaning the file not exist) 'continue script execution'
    -------------

    ... about the .plist file ...
    6)
    who can help me to understand and correctly configure the plist file ??
    what these values means ??

    (NOTE: The values below are taken from the testuser link in this tread)
    {
    Description = "IPAliasing";
    Provides = ("Secondary IP Addresses");
    Requires = ("Resolver");
    Uses = ("Network Time");
    OrderPreference = "None";
    Messages =
    {
    start = "Starting IP aliasing";
    stop = "Stopping IP aliasing";
    };
    }


    i hope i was sufficently explicit ... thanks a million to everyone can help me !!
     
  7. blb

    blb `'

    Joined:
    Apr 9, 2001
    Messages:
    651
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    CO, USA
    Your supposition is correct; it is basically like an include, variables become available to the current script, any commands in that referenced script are run, etc.

    You can do this in Bourne, use test, which should also be the program known as [ (try a whereis [ sometime).
    This should work in Bourne on any OS:
    Code:
    test ! -d tempdir && mkdir tempdir
    
    to create the directory if it doesn't exist. See man test.
     
  8. Zeus

    Zeus Registered

    Joined:
    May 11, 2001
    Messages:
    243
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Italy
    At first i wanna thanks all people (a particular thanks goes to testuser) who posted and helped me to develop my little script.
    Unfortunatly for you [ :))) ] now i have another question...
    i've set my script so that if a defined directory is not find the script stop itself and exit.
    Now is it possible that when (and if ) this appen to see a message in the mac os x boot box ??

    thanks
     
  9. Zeus

    Zeus Registered

    Joined:
    May 11, 2001
    Messages:
    243
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Italy
    This is the incriminate script ... the author is convitc to have developed this script in 'hush hush' vay !!! :)))

    This script provide the vm swap on any volume named '.swap' regardless for its /dev/disk filesystem. Infact this script don't use any mount command or fstab file ... but let autodiskmount mount all available volumes and then if the script find a mount point named /Volumes/.swap then dynamic_pager will create its file on that disk. If the mount-point doesn't exist swapfiles will be placed in the Apple default directory (/var/vm/).


    ---------------------- SCRIPT -------------------
    #!/bin/sh

    . /etc/rc.common


    ConsoleMessage "Starting Virtual Memory"

    swapdisk=/Volumes/.swap
    swappath=${swapdisk}/vm
    swapfile=${swappath}/swap_file_

    ##
    # Make sure swap disk exist and has been mounted
    ##

    if [ -d ${swapdisk} ]; then

    ##
    # Cleaning swap disk from eventual Mac OS 9 Destop Database Files
    ##

    if [ -f ${swapdisk}/Desktop\ DB ]; then rm -f ${swapdisk}/Desktop\ DB; fi
    if [ -f ${swapdisk}/Desktop\ DF ]; then rm -f ${swapdisk}/Desktop\ DF; fi

    ##
    # Preparing disk for hosting swapfiles
    ##

    rm -rf ${swappath} > /dev/null
    mkdir -m 740 ${swappath}
    chown root:wheel ${swappath}

    ##
    # Swap in swap partition
    ##

    dynamic_pager -H 40000000 -L 160000000 -S 52428800 -P 2 -F ${swapfile}

    else

    ##
    # Swap disk not mounted !! Swapping on the Mac OS X defallt position
    ##

    rm -rf /var/vm > /dev/null
    mkdir /var/vm

    dynamic_pager -H 40000000 -L 160000000 -S 52428800 -F /var/vm/swapfile

    echo "ATTENTION: Please note: during boot the swap disk was not found. Check your swap
    partition \n ATTENZIONE Durante l'avvio del sistema non e' stato possibile
    utilizzare il
    volume di swap. Controllarne lo stato!" > /VM_Problems!!.txt

    ConsoleMessage "PROBLEM: Can't find swap disk!"
    sleep 60

    fi



    ----------------- StartupParameters.plist ------------------
    {
    Description = "VirtualMemorySystem";
    Provides = ("Swap");
    Uses = ("Disks");
    OrderPreferece = "Early";
    Messages =
    {
    start = "Starting Virtual Memory";
    stop = "Stopping Virtual Memory";
    restart = "Restarting Virtual Memory";
    };
    }
    ---------------------------------------------------

    Question:
    Now, how to set the script in way that if the else condition is processed the mac os X BootPanel will show somethig to make me able to understand that the swap is in the / disk and not /Volumes/.swap as i like ???
     
  10. blb

    blb `'

    Joined:
    Apr 9, 2001
    Messages:
    651
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    CO, USA
    The avoidance of running dynamic_pager is a good one (someday, Apple should get around to documenting it), as I think I once killed my machine running it a second time...

    However, you're running /etc/rc another time by dotting it in the startup script. I'm not sure what effects this may have, as there are a number of things that should just error out, but you never know.

    I would have the dynamic_pager stuff commented out as you did, but instead of dotting /etc/rc in the startup script, look to see if dynamic_pager is running already; so replace

    Code:
    if [ ${swapdir:=not} = "altered" ]; then
    
    with

    Code:
    ps auxw |grep -v grep |grep dynamic > /dev/null 2>&1
    if [ $? == 0 ]; then
    
    so that block only runs dynamic_pager if it wasn't started already by /etc/rc.
     
  11. Zeus

    Zeus Registered

    Joined:
    May 11, 2001
    Messages:
    243
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Italy
    as already written i'm new to unix ... so excuse me if i' m 'pissing-out' !!

    probably a good solution can be this

    getting the PID of dynamic_pager using ps

    if there is a PID we can kill it

    (so even if rc has changed, reintroducing the dynamic_pager initialization, there is no problem)

    then we can remove the old swapfiles

    all done ! now that the rc started dynamic_pager is killed ... i think it's possible to restart dynamic_pager so that it swap on a different Volumes.





    P.S.

    4 testuser:
    thanks for the log-introducing tip ...

    and ...
    ----------------------
    I would appreciate any feedback from scripting pros out there.
    --------------

    What does it mean ?? (don't forget i'm italian... )
     
  12. Zeus

    Zeus Registered

    Joined:
    May 11, 2001
    Messages:
    243
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Italy
    during script execution terminal report an error:

    ./VM: parse error near `\n' [82]


    the problem is that line 82 is the end of file and is empty !!

    why ?? what does it mean ??
     
  13. Zeus

    Zeus Registered

    Joined:
    May 11, 2001
    Messages:
    243
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Italy
    testuser,
    excuse me for your lossed 'uptime' ... i must send you my best compliments for all your posts ... for what i can see they are more then 1000 ... WOW !

    I think this script is very 'cazzuto' !! ;-))) ... incredibily cazzuto !!!!
    'cazzuto' is an italian way to emphatize something's joystick ( intended as the male attribute!!) :)))))


    I 've just two suggestion:
    1) why don't we move the swapreport file il the /var/log directory with other logs ??
    2) abut that damned dynamic_pager ... if you look at the vm_stat results you 'll notice the tha page size is 4096 ... so i think the swapfile size should be a multiple of 4096 ... not 4000 like apple did !!

    I've anothe news 4 u ... at the terminal prompt type
    dynamic_pager -h


    ... it's not very much ... but is something .... now we should just know what * water alert trigger is ... :))
    --------------------------

    abuot your previously post:
    code:
    ------------------------------------------------------------------------
    {
    Description = "Startup Virtual Memory System";
    Provides = ("swapmounter");
    OrderPreferece = "Late";
    Messages =
    {
    start = "Starting Virtual Memory";
    stop = "Stopping Virtual Memory";
    };
    }
    ------------------------------------------------------------------------
    NOTE: Setting the OrderPrefernce to Early will make your computer hang (don't ask me how I know). This is probably because the volumes are not mounted when an early script runs, or that it gets called before the /etc/rc script. If someone has a better explanation, please let me know. The Uses key is not included since other services are irrelevant to this script.

    Infact, it's like you supposed ... if you set Orderpreference to early you will need to define the 'Uses' value to Disks ... if you do so, you will tell your script to be executed 'early' but you will tell to systemstarter to load first the /System/library/StartupItems/Disks script that launc autodiskmount ....

    so change your StartupParameters.plist file in this way


    {
    Description = "VirtualMemorySystem";
    Provides = ("Swap");
    Uses = ("Disks");
    OrderPreferece = "Early";
    Messages =
    {
    start = "Starting Virtual Memory";
    stop = "Stopping Virtual Memory";
    restart = "Restarting Virtual Memory";
    };
    }


    This is the Italian localization file content ...



    Avvio Memoria Virtuale (start)
    Arresto Memoria Virtuale (stop)
    Riavvio Memoria Virtuale (restart)
     
  14. Zeus

    Zeus Registered

    Joined:
    May 11, 2001
    Messages:
    243
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Italy
    the idea to make an installer is grat ...

    but first i think we should find some news about -H and -L options of Dynamic Pager this beacuse i prefer to set the swap partition on my mac to 50 Mb instead of 80 Millions ... this because i have a 512 Mb swap Partition and if the swap file is 80 Millions in high swap (pageins) conditions there will be an unusable disk space (if i have 60 MB free and 80Mb swapfile cannot be created, a 50Mb can !

    Also you should try to set a plus priority value and stess your mac ... i've noticed that doing so if you have (it's my case) a NIS server with FTP access abled with NAT active your mac will be more responsible ... test it if you can ... and let me say what you think (ohhh noooo you will' lost (or loose ???i dont't remenber english grammar very well as u can see) you 'uptime' another time !!!

    Probably (tell me what you think) in the script the 'mount -uw /' command line is not very useful ... specially if you consider that autodisk mount has already runned (so mount-points are already present and active) and the swap files aren't in the / drive ...

    For the installer i think it will be a good idea to put a default /etc/rc file without swap anyway ... this will' avoid in normal situations to load unuseful command ... obviously we should save the old rc file (if someone has modified by himself) as for ex. as /etc/rc.old

    about the installer ....
    if we wanna let the user to easyly set his swap volume we should also create a file containing the swap target mount point ... and possibily to let the user change it when he wants ... but i've no idea on how to do it and how to make possible the script read that file and set output file as variable ... probably this is too complex ... let it go ...

    about the scripts
    how to set ps so that it rend just the pid value ?? i've tryed some way ... but i'm no good for this !! :-((((



    P.S. have you got any ideas of where (or who) can tell us more about the -H and -L dynamic_pager's options please ??


    P.S our script is great !!!! don't you think so??? :)))))
     

Share This Page