[SOLVED] Running a startup script as a user at boot once

Hello!
So, as the title suggests, I am trying to start a script as a specific user at boot. To avoid accidentally starting it multiple times, I don't want it to run every time to user logs in. So far I've looked into creating a custom systemd service to call a script as root then using 'sudo -u user script' to run said script.
This almost worked, the script must have executed but, because it was starting a new task in a screen, it created a dead screen. Although I'm not entirely sure why (I suspect it has something to do with not running screen as the logged in user), I figured I would ask if anyone knew of a better way to do this. My solution so far is pretty convoluted.
Side notes: This is running on a GUI-less server (hence the possibility of a user logging in multiple times or having multiple sessions open at once)
Last edited by Lindenk (2013-07-09 15:33:45)

Sure,
Here's essentially the unit file -
[Unit]
Description=Runs a script to run a script to start a script to start a server
[Service]
ExecStart=/absolute/path/to/a/rootScript.sh
[Install]
WantedBy=multi-user.target
And the root script
#!/bin/bash
#This script will run another script as the user 'server'
sudo -u server /path/to/userScript.sh
And the user script
#insert possible setup stuff here
screen -S serv -d -m /path/to/server/start.py
#insert possible cleanup stuff here
Also as a side note, I did already allow root to run commands as server without a password.
EDIT: Another side note, the root script and user script do in fact work as intended when started individually. When running through systemd (at boot or not), the screen created is dead.
Last edited by Lindenk (2013-07-09 14:36:54)

Similar Messages

  • Run the startup script (start and stop) with a user other than oracle

    hi
    I wrote a script that stops and start oracle. the connection to the db was done : sqlplus "as sysdba".
    The script works fine if oracle user runs it. however, I need another user to run this script also. The script does not run with this user even though I added it to the oracle group.
    any idea please?

    try the -c option with the su like this:
    su - oracle "-c scriptyouwrote.sh"
    greetings

  • Bypassing startup script in .cshrc.user file for console login

    Hi,
    I'm calling a startup script in my .cshrc.user file. How do I bypass the startup script if it is a console login. Other cases like telnet, rlogin or opening a new terminal should go through the startup script.
    Thanks,
    Ramkumar

    Short note; instead of ..
    echo `tty` >> testfile
    you could just do..
    tty >> testfile
    .. since the "tty" command sends its output to STDOUT, same as "echo", and >> takes the STDOUT and puts it in a file.
    Anyway, what does "tty" give you if you run it on the console?
    //Magnus

  • Run a powershell script on every user login/unlock

    Hi everybody
    I need to run a powershell (that opens an interactive dialog box) when every user log in or unlock session or remote connection to this server.  In other words it have to be run every time a user enters a credential. 
    I tried task scheduler, but it didn't work properly. I tried triggers like "on workstation unlock" and "on connection to user session". I change user and group to administrators but still it runs under the author user (me) . 
    I found out user login event id is 4801 and 4778. can I use these event ids to run powershell code? Can I check raising the event ids through my code? 
    any other idea please?
    Thanks in advance

    Im not going to block a logon! I want to run a powershell code for every user login. assume like a message after user login. this kind of message for me is showing to me after other users login.
    specifically this is a window from that I designed to execute on user login to show on the server and every user writes down in the text box the changes that is making on the server. this is a changes log solution to me. then I need to show every user to
    work properly.
    You can run a scheduled task at logon that can display a message.  You can define this task in Group Policy.
    You can also just use the standard user logon message which can also be defined in Group Policy.
    You can execute a script task whenever an unlock event happens.  This, too, can be defined in Group Policy.
    ¯\_(ツ)_/¯

  • [solved] Running a systemd script as root on startup

    Hello,
    I wrote following shell script in order to restart vsftpd when my dynamic wan ip adress changes:
    #!/bin/bash
    oldip=`xxxdyndns.com +short`
    while :
    do
    newip=`dig xxxdyndns.com +short`
    if [ $newip != $oldip ]; then
    echo "renewd ip. new ip: $newip"
    oldip=$newip
    systemctl restart vsftpd.service
    fi
    sleep 5m
    done
    exit 0
    The script needs to be run with root privileges (due to the systemctl restart line).
    I want the script to be run when my system starts, so I thought about writing a systemd service file. Unfortunately, I could not find out how to tell systemd that it shall run the script as root. I'm sure that this is a very novice question, but my board and google search did not tun out to be successful.
    Last edited by vibee (2014-01-16 15:42:02)

    Have you tried a service with
    User=root
    similar to e.g. https://wiki.archlinux.org/index.php/Sy … ces#Gitlab ?
    Last edited by karol (2014-01-16 15:28:37)

  • [solved] run program before network is available and stop boot on fail

    Hi everyone,
    Let's say I have an program that is absolutely essential to my system, and I want it to run before netctl even tries initialising my network connection (in fact, I want the network to be unavailable until the program has finished its execution). I _always_ want to see the program's output on the console, and if it exits with a non-zero exit code, I want the whole boot process to be aborted (or at least all of netctl's units to be disabled). Is this possible in systemd?
    Last edited by popoffka (2014-04-18 13:07:09)

    popoffka wrote:Hi everyone,
    Hi.
    popoffka wrote:Let's say I have an program that is absolutely essential to my system, ...
    Sure.
    popoffka wrote:and I want it to run before netctl even tries initialising my network connection (in fact, I want the network to be unavailable until the program has finished its execution).
    Use 'Before=' in your unit file. Make sure it does not fork.
    popoffka wrote:I _always_ want to see the program's output on the console,
    Pipe it's output to /dev/console directly. Maybe there is a cleaner why, I'm not sure. SystemD actually supports the redirect directive in it's native language (nice).
    popoffka wrote:and if it exits with a non-zero exit code, I want the whole boot process to be aborted (or at least all of netctl's units to be disabled)
    Use 'RequiredBy=' in your unit file. That could be used to prevent netctl from starting. Preventing the whole boot isn't a bit drastic isn't it?
    popoffka wrote:. Is this possible in systemd?
    Yes. Consult the systemd.service and systemd.unit manpages.

  • Systemd startup script - half of the commands run, half don't??

    i have a previous rc.local file which was a startup script on a router machine i have. it is designed to run tcpdump commands for the interfaces and output them to terminals tty9, tty10, and tty11. it worked perfectly...
    i have since transitioned to systemd. I followed wiki instructions to make a oneshot service file and have enabled it. here is the file: (/usr/lib/systemd/system/startup.service)
    [Unit]
    Description=Startup Script
    After=iptables.service
    [Service]
    Type=oneshot
    ExecStart=/etc/scripts/startup
    [Install]
    WantedBy=multi-user.target
    here is the script /etc/scripts/startup: (email addresses disquised)
    #!/bin/bash
    route add -net 10.11.12.0/24 gw 172.16.16.4
    /usr/sbin/tcpdump -pnq -i external >> /dev/tty11 &
    /usr/sbin/tcpdump -pnq -i internal >> /dev/tty10 &
    /usr/sbin/tcpdump -pq -i wifi >> /dev/tty9 &
    echo "$HOSTNAME system startup complete `date '+%r %x'`" | /usr/bin/mailx -c "[email protected]" -s "$HOSTNAME Startup Message" [email protected]
    here is the output from the startup command with journalctl:
    Nov 18 22:46:10 pLAN9-Gateway systemd[1]: Starting Startup Script...
    Nov 18 22:46:10 pLAN9-Gateway systemd[1]: Starting OpenSSH Daemon...
    Nov 18 22:46:10 pLAN9-Gateway systemd[1]: Started OpenSSH Daemon.
    Nov 18 22:46:13 pLAN9-Gateway startup[409]: . . . message not sent.
    Nov 18 22:46:13 pLAN9-Gateway systemd[1]: Started Startup Script.
    the route add command runs, but nothing else does. as you can see, there is absolutley NOTHING helpful in the log, other than the completely useless "message not sent" output from mailx. running the startup script manually with "systemctl start startup" works fine.
    iptables.service is setup to run After=netcfg.service, so I would assume that beucase the startup script runs after iptables, it should run after netcfg as well.
    why aren't these commands running?

    A oneshot service is killed once the main process returns (that's why it is called a "oneshot"). Your tcpdump processes are killed once startup returns.

  • Startup Scripts for OBIEE 11g on Linux

    Hi, I originally spent many hours trying to find a startup/shutdown script for OBIEE on linux, in the end I compiled a new one based on notes in the install manual and other posts on the subject until I got it working consistantly
    Please add comments or improvements :)
    Note: you need to create the boot.properties file (in /security) for each server, and provide the username/password so WebLogic won't prompt for it when starting automatically (otherwise it doesn't start :p) ....refer to the install manual or [weblogic boot.properties|http://onlineappsdba.com/index.php/2010/08/21/weblogic-startup-prompting-from-username-password-bootproperties/]
    #!/bin/bash
    # /etc/init.d/obiee
    # Run-level Startup script for OBIEE
    # set required paths
    export ORACLE_BASE=/opt/oracle
    export ORACLE_HOME=/opt/oracle/product/11.1.0/db_1
    export ORACLE_OWNR=oracle
    export ORACLE_FMW=/opt/oracle/product/fmw
    export PATH=$PATH:$ORACLE_FMW/bin
    case "$1" in
    start)
    echo -e "Starting Weblogic Server...."
    su $ORACLE_OWNR -c "$ORACLE_FMW/user_projects/domains/bifoundation_domain/bin/startWebLogic.sh > /dev/null 2>&1 &"
    sleep 30
    echo -e "Starting Node Manager..."
    su $ORACLE_OWNR -c "$ORACLE_FMW/wlserver_10.3/server/bin/startNodeManager.sh > /dev/null 2>&1 &"
    sleep 30
    echo -e "Starting Managed Server: bi_server1..."
    su $ORACLE_OWNR -c "$ORACLE_FMW/user_projects/domains/bifoundation_domain/bin/startManagedWebLogic.sh bi_server1 [url for admin console] > /dev/null 2>&1 &"
    sleep 30
    echo -e "Starting Components...."
    su $ORACLE_OWNR -c "$ORACLE_FMW/instances/instance1/bin/opmnctl startall > /dev/null 2>&1 &"
    sleep 30
    stop)
    echo -e "Stopping Components...."
    su $ORACLE_OWNR -c "$ORACLE_FMW/instances/instance1/bin/opmnctl stopall > /dev/null 2>&1 &"
    sleep 30
    echo -e "Stopping Managed Server: bi_server1..."
    su $ORACLE_OWNR -c "$ORACLE_FMW/user_projects/domains/bifoundation_domain/bin/stopManagedWebLogic.sh bi_server1 [url for admin console] [weblogic user] [weblogic pass] > /dev/null 2>&1 &"
    sleep 30
    echo -e "Stopping Weblogic Server...."
    su $ORACLE_OWNR -c "$ORACLE_FMW/user_projects/domains/bifoundation_domain/bin/stopWebLogic.sh > /dev/null 2>&1 &"
    sleep 15
    status)
    echo -e "Component Status...."
    su $ORACLE_OWNR -c "$ORACLE_FMW/instances/instance1/bin/opmnctl status"
    restart)
    $0 stop
    $0 start
    echo "Usage: `basename $0` start|stop|restart|status"
    exit 1
    esac
    exit 0
    Hopefully this helps others in the same situation
    Cheers

    HI,
    Is this script for Enterprise Install on a single computer? For Simple install I think there is no concept of nodemanager and managedserver.
    --Joe                                                                                                                                                                                                                                                                                                                           

  • Running a shutdown script

    Does anyone know how to get OSX to run a unix script when a user selects "Log Out" from the Finder?
    I have a script that cleans up a bunch of files upon logout and I'd like to have it run automatically when a certain account logs-off.
    Can this be done?
    Thanks in advance
    Eric

    Eric--
    I believe you'd do it using loginwindow hooks. Apple's developer site has some information on how to do it. For 10.3 and later, you should be able to do this for a logout hook:
    sudo defaults write com.apple.loginwindow LogoutHook /path/to/scriptI've never tried it, though, so I'm not speaking from personal experience...
    charlie

  • [SOLVED] Why some commands run properly in startup script others don't

    I am trying to finish setting up my MythTV box. I have the following commands to run at startup:
    #!/bin/sh
    # Stop spamming dmesg
    echo -n 0000:00:06.1 > /sys/bus/pci/drivers/ehci_hcd/unbind
    # Enable wake from remote
    echo enabled > /sys/bus/usb/devices/4-5/power/wakeup
    # Link optical drive to /dev/dvd (needed for myth)
    if [ ! -e /dev/dvd ]; then ln -s /dev/cdrom /dev/dvd; fi
    # Start LIRC
    echo lirc > /sys/class/rc/rc0/protocols
    ln -sf /run/lirc/lircd /dev/lircd
    lircd -d /dev/lirc0 /etc/lirc/hipermc
    # Start mythbackend
    mythbackend &
    # Start LCD server
    LCDd &
    I have tried making this part of rc.local and also as a startup item in XFCE (with script called using passwordless sudo since it requires root permissions). In each case, only some of the commands run properly (the dmesg, wake from remote, and start LIRC sections). The other commands don't seem to work. However if I run the script manually after the computer has finished booting, everything works perfectly. Can anyone help me figure out what's going wrong?
    It seems like path is already set since some commands do work without the full pathname. Also please don't suggest using systemd for starting the daemons - it does not work for me with LIRC or lcdproc and there is a driver bug with my hardware that keeps me from using the mythbackend systemd service.
    Thanks in advance for the help.
    Last edited by mosquitogang201 (2012-12-16 14:42:11)

    The exec command was what I needed. Was able to follow the error messages to find out the command im using under "stop spamming dmesg" was messing everything else up. Also I am running systemd - just not using it to start these items since the systemd service files weren't working reliably.
    Anyways this brings me to another question - is there a more elegant way to stop dmesg from getting spammed? About 5 times per second I get the error
    hub 2-4:1.0: unable to enumerate USB device on port 3
    which makes it very difficult to look through dmesg logs to find an actual problem.
    EDIT: Just needed to add the sleep command after the dmesg section to give the kernel enough time to reload using ohci_hcd. Now script is working perfectly. Marking as SOLVED.
    Last edited by mosquitogang201 (2012-12-16 14:48:47)

  • [SOLVED] Running a script at startup

    Hi,
    I have a script that I've developed and I want it to run when the machine boots up. I would use gnome-session-properties, but I want it to run even if I don't log in to the machine. An additional condition is, that it should run as user, not root. It's optional but there's no reason for it to run as root.
    I've tried putting it up in /etc/rc.local like this:
    kuba:~$ cat /etc/rc.local
    #!/bin/bash
    # /etc/rc.local: Local multi-user startup script.
    su -c "/home/kuba/devel/sabcommander/backend/backend.rb" - kuba &
    And it actually spawns the processes:
    kuba:~$ ps aux | grep sabcommander
    root 648 0.0 0.0 27588 1096 ? S 21:21 0:00 su -c /home/kuba/devel/sabcommander/backend/backend.rb - kuba
    kuba 1020 0.1 0.6 106452 26280 ? Sl 21:21 0:00 ruby /home/kuba/devel/sabcommander/backend/backend.rb
    But they don't seem to work. This script connects via WebSockets to a remote server and keeps the connection, when it receives commands it executes few HTTP requests at localhost.
    When I run a separate instance from the terminal, I then see that this script works, so I'm sure the one started from rc.local is not working.
    Thanks
    Last edited by farnoy (2012-05-08 06:26:13)

    funkypotatoe wrote:
    farnoy wrote:
    Thanks for replies,
    I'm starting network like this:
    DAEMONS=(syslog-ng dbus @net-profiles crond ...)
    I'll rewrite my script to sleep for a few seconds at the beginning and then I'll post here.
    I think to start net-profiles in the background is not the best idea.
    It most definitely isn't, if a script depending on it is to be started during the boot process 
    I think that's the problem right there, remove the @ sign in front of network-profiles and try again.

  • [Solved]Run shell script as root on startup

    Hi
    How to start shell script as root on startup?
    When i search online it is all about an rc.local, but I do not have a rc.local, and when I make one myself it does not run the shell script.
    Regards
    Frederik
    Last edited by Fred7109 (2015-05-26 20:13:04)

    Thankyou for the answers, I went into problems so could first answer now. The script is supposed to delete sandboxes I have created with the sandfox script, and also start a new sandbox with sandfox.
    Regards
    Frederik

  • Need help writing a script to delete a folder to run at startup

    I have no experience with this and need full assistance to set this up.
    I am hoping to write a script which can be run at startup on a single client machine which is giving me font trouble. Our client machines are network controlled by our server and have multiple users all in the Server user database.
    The fonts are behaving badly until we trash this folder HD>Library>Caches>com.apple.ATS
    On restart the fonts behave as expected.
    Can someone help me write a script or give me a command I can insert in a crontab with Cronnix to delete this folder at startup?
    Thanks

    They both work fine (read that as: it can be added into either place) While that message in crontab seems to intimate that Apple will do something drastic like disable or make it hard to run cron I cannot see how, cron has been in all *nix's since the beginning so to remove it would anger quite a few people...
    Having said that, If you want to make a launchd item you would put the below into /Library/LaunchDaemons/ create a file (call it someting memorable like 'Delete_com.apple.ATS') put this into it:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
    <key>Label</key>
    <string>Delete com.apple.ATS</string>
    <key>ProgramArguments</key>
    <array>
    <string>rm</string>
    <string>-rf</string>
    <string>/Library/Caches/com.apple.ATS</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    </dict>
    </plist>
    and finally load it...
    PowerMac G5 DP 2.5Ghz   Mac OS X (10.4.3)  

  • VB startup script not running on Win7

    Hi,
    my startup VB script not working on Windows 7. The script should install or uninstall office 2010 based on group membership. 
    I tested scipt on Windows XP and everything works. When I run script manually, it works.
    Quite similiar batch file works, office are installed but not same functionality for me.
    I turn UAC off, bot not working.. Also add dword EnableLinkedConnections  but nothing change.
    Batch file works but VBScript not!
    Any advice? Thx

    Hi Tomas Cink,
    Based on my understanding, the start VB script which is deployed via GPO could not works fine on Windows 7 clients, although it can works fine on Windows XP clients and it can
    run sucessfully if manually. Right?
    Firstly, please run the following command on the problematic workstation.
          Gpresult /h > C:\temp\gpresult.html  (“C:\temp\” is the path of the gpresult.html, you can set it by yourself)
    This file is used for checking the result of Group Policy information. Open the file to check if the GPO has applied.
    If the GPO has applied, please follow the following step to narrow down the issue:
    Change the script to the following command: add  > %SystemDrive%\Users\test.txt
    to the script.
    Log on to a client with a regular user account, run
    gpupdate/force, and then re-log on to this client.
    Check if the test.txt file exists in this location
    %SystemDrive%\Users.
    If the
    test.txt file exists, the GPO with startup script is applied successfully. But the result of running logon script achieves the goal we excepted or not which is still not sure.
    Regards,
    Lany Zhang

  • Group Policy Startup Script Applies My Policy But Does Not Run The Acual Scripts

    I have created a basic batch file with msiexec.exe to uninstall a program on startup and then another separate .bat script to install the same program but the newer version. The software I'm referring to has to be completely uninstalled BEFORE
    I install the "newer" version of the same program, it cannot just be overwritten. If I run a gpupdate /force on the client computer and restart, the scripts run as they are supposed to and everything works but the problem is that I can't get it to
    run on first boot on a computer that has been turned off for months, even after multiple reboots it still doesn't run the scripts. The 3 policies apply to the different computers/users but the scripts don't run. I manage a theme park that is
    only open 4 months of the year so the rest of the time the in park PC's are turned off. I have created my OU as "POS Computers & Users" which has all of the computers and users that will take this policy. I also have 3 Group Policy
    Objects attached to this OU in Group Policy, 1 is the program uninstallation .bat script policy that runs on startup, 2 is the install .bat script policy that runs after the uninstallation script, and 3 is the Default Policy for the OU. I already have the
    "Always wait for the network at computer startup and logon", "Run startup scripts visible"enabled, "Run startup scripts asynchronously" disabled, and "Run Logon Scripts Synchronously" enabled for all 3 of the
    policies. They are all "link enabled" and security filtering is set to only the OU I mentioned earlier so that it doesn't affect anyone else. I have the link order set as the script I want to run first as the last and the one I want to run last first
    because from what I understand inheritance is from bottom to top. The install file is accessible by everyone with full permissions on our "Shared" drive so I know its not a permissions issue because it runs after a gpupdate /force with a restart.
    The scripts are in the proper folder for the policies they are attached to and permissions are fine.
    Here is my uninstall .bat script (msiexec.exe /X{14324A6A-BDD1-4F40-8E77-664C8AEEA251} /forcerestart /qb-! ALLUSERS=1 REMOVE=ALL)
    Here is my install .bat script (msiexec.exe /i {\\kksrvad\shared\Gatemaster\NewGatemaster.msi} /qb ALLUSERS=1)

    Can't be done in a login script.
    This is a Group Policy issue and not a scripting issue.  You do not have a script.  You have a command saved in a batch file and you are using a GPO.  Not a scripting issue.
    ¯\_(ツ)_/¯

Maybe you are looking for

  • IMac no longer seeing Lacie external HD

    Hello =) I'm hoping that some smart cookie here can help me. My iMac no longer sees the Lacie firewire external HD that I've had connected to my computer for the last year or so. I don't know exactly when it stopped seeing the drive, but it's a probl

  • Tcode  S_ALR_87012301  Report RFSUSA00 after upg ecc 605 without page break

    Hi ! This G/L account balances report (Tcode  S_ALR_87012301 : Report RFSUSA00 ), in version 4.6C, have a page break by the subtotals level defined at the selection screen. Now after the upgrade with ecc 605, the output is continuous... I checked the

  • ESS: Employee Self Services,  Hrxssce_Service_Appl_Path does not exist

    Hi, I am having real problems implementing the employee self services in a Portal environment. It is located on a NW 7.0 JavaAddin with SP 15. I followed the configuration guides on help.sap.com and rechecked all settings. I could also find some thre

  • Ipod touch isnt organizing music (artists and albums) and no search bar

    my ipod touch stopped organizing the artists and albums in the music application since the last time i synced music... so when i go to music, artists, i see the artists not organized in letters.. it used to organize them, it used to put each artist i

  • Frustrated with iTunes Store music file quality.

    Dear Apple iTunes Store supervisors,      Ive decided I will no longer buy music from itunes until you start to release music in a better quality, uncompressed, lossless-format and higher bitrate and sample depth.  The list of albums i want to buy gr