Run script on boot



  • Hi all,
    I would like to run a simple bash script just after booting my Nexus 5. I tried to use cron job or rc.local file (saw it in a couple of tutorials) but I am having problems when editing these files. Could it be because it is a read-only file system? Is there any other way to run scripts on boot then?
    Thank you in advance!



  • What is your end goal exactly? You need to run something as the phablet user?

    You can create an upstart job to run your script in ~/.local/share/upstart/ perhaps.



  • @dobey It is just to hide android (anbox) apps from the drawer; I have a script that I run everytime that I boot the phone, and would like to automate this.
    How exactly does this .local/share/upstart work? There is no upstart existing script or folder as I see it. And how should I call the script from there? Sorry, I am a little bit lost 🙂 Thanks!



  • @thrill_seeker Or maybe it is in ~/.config/upstart/ instead. I don't recall exactly.



  • @thrill_seeker Oh wow what a nice script ! Could you share this please ?



  • @thrill_seeker hi, as @dobey said you should write a simple .conf file to be stored in .config/upstart location. The content of the file should be something like the following:

    description "script by yourName"
    
    start on started unity8
    
    respawn
    
    exec /linkToYourScriptLocation/yourScript.sh
    
    

    The above script is meant to be executed at the fist start-up of the device (when unity8 is started). Please notice this is the simplest as possible .conf file to accomplish the only duty of executing your script. To have additional information on the further commands available, you might be interested in reading the Upstart manual you can find on the internet.



  • @matteo I would not include respawn there for something only meant to run on boot. Otherwise it will simply keep re-running it I think.



  • Hi again, I am sorry but I tried exactly what you said and does not work 😞 I think that maybe anbox is started after unity8 as well, and the script may be run before anbox launches?

    I am not really sure, but thank you very much anyway for your time!



  • @Emphrath Of course! Here you have:

    #!/bin/bash
    
    # Set variables
    filelist_dir=/home/phablet/Documents/anbox
    bk_dir=/home/phablet/Documents/anbox/bk
    files_dir=/home/phablet/.local/share/applications
    filelist=list_files.txt
    
    # Delete dir bk if exists and create it again
    rm -rf $bk_dir
    mkdir $bk_dir
    
    # Get string of files to mv from filelist
    files=$(cat $filelist_dir/$filelist)
    
    # Append files_dir to each file
    files_to_mv=$(for file in $files; do echo $files_dir/$file; done)
    
    # Move all files from files_dir into bk_dir
    for file in $files_to_mv; do mv $file $bk_dir; done
    

    This file is called desktop-delete.sh, and reads a file called list_files.txt that contains all apps to hide. In my case, it contains the following (see .local/share/applications for all apps):

    anbox-com-android-inputmethod-latin.desktop
    anbox-com-android-inputmethod-latin.png
    anbox-com-android-calculator2.desktop
    anbox-com-android-calculator2.png
    anbox-com-android-calendar.desktop
    anbox-com-android-calendar.png
    anbox-com-android-contacts.desktop
    anbox-com-android-contacts.png
    anbox-com-android-deskclock.desktop
    anbox-com-android-deskclock.png
    anbox-com-android-documentsui.desktop
    anbox-com-android-documentsui.png
    anbox-com-android-email.desktop
    anbox-com-android-email.png
    anbox-com-android-gallery3d.desktop
    anbox-com-android-gallery3d.png
    anbox-com-android-music.desktop
    anbox-com-android-music.png
    anbox-com-android-settings.desktop
    anbox-com-android-settings.png
    anbox-org-chromium-webview_shell.desktop
    anbox-org-chromium-webview_shell.png
    

    I have both files contained in this path: Documents/anbox. The file delete-desktop.sh should be executable (do chmod a+x Documents/anbox/delete-desktop.sh).

    Having all this, is just a matter of executing the script (./Documents/anbox/delete-desktop.sh) and should work fine!



  • @thrill_seeker

    One way to check if the script is run, but then Anbox regenerates the files is to check the timestamp on the files after a reboot. If they are from before the reboot, then your script was not executed for whatever reason. Otherwise, you might be right, in which case you could add a delay (sleep xx) in your upstart job before your script is executed.



  • @thrill_seeker awesome ! thx



  • @arubislander Good point! I tried it, but to no avail u.u anyway, thank you for the tip!



  • What does exacly exec do?

    I ask, because sh doesn't work for my script like . does.

    I'm also interested as I want to run VPN on boot and keep it always on.

    Here are my scrips and other information.

    Also, what are the nmcli commands that enable cellular data? I tried to find it and I'm run out of ideas.


Log in to reply