command line - How to run a batch file to run at any user log in to Ubuntu 10.04

  • whizkid

    Please help me create a batch file that I want have it configured every time any user logs in to my personal computer (there are couple of non-admin non-root users) Another concern is that this should run invisible, i.e. no prompts, etc.

    I want to have this command run invisibly at every user login to my PC : sudo iptables -A OUTPUT -p tcp -m owner --uid-owner anotheruser -j DROP

    Basically, this command stops internet access for this user 'anotheruser'. And I want to have this command run every time this (or any other user) logs in to my PC.

  • Answers
  • Matthieu Cartier

    If you didn't need it to run as root, you'd be best to write it as a script and put it in /etc/profile.d/. profile.d scripts are executed on login. However, since you need it to run as root invisibly, you'd probably either need to:

    • Make a daemon to do it
    • Have root run a while loop for user logins

    There's probably some other ways too.

  • Related Question

    windows 7 - How to create batch file that delete all the folders named `bin` or `obj` recursively?
  • Nam G. VU

    I have the need of deleting all bin & obj folders in a folder on my PC. So, I'm thinking of a batch file to do that but I'm not famaliar with batching file in Windows. Please help.


    After discussion with user DMA57361, I got to the current solution (still having problem though, see our comments):

    Create a .bat file, and paste the below command:

    start for /d /r . %%d in (bin,obj) do @if exist "%%d" rd /s/q "%%d"


    start for /d /r . %%d in (bin,obj) do @if exist "%%d" rd /s "%%d"

    @DMA57361: When I run your script, I get the below error. Any idea?

    alt text

  • Related Answers
  • DMA57361

    This has been previously answered over on Stack Overflow, which is where I've taken the main thrust of this answer from.

    Try the following command, you can run it from within cmd:

    for /d /r . %d in (bin,obj) do @if exist "%d" rd /s/q "%d"

    If you need other folders to be changed then just add new items to the (bin,obj) set in the middle of the command.

    This will delete everything matched without warning and without using the recycle bin - so, if you want a bit of extra safety, drop the /q from the call to rd at the end, and the system should ask you before each deletion.

    for /d /r . %d in (bin,obj) do @if exist "%d" rd /s "%d"

    If you intend to run the command from within a batch file you will need to replace every instance of the variable %d with %%d, like so:

    for /d /r . %%d in (bin,obj) do @if exist "%%d" rd /s/q "%%d"
    for /d /r . %%d in (bin,obj) do @if exist "%%d" rd /s "%%d"

    As per the conversion had in the question comments

    If the command refuses to run in a batch file (unable to replicate here), try prefixing the command with start. Althought this will start the process to run concurrently with the batch file, which may cause other issues, it seems more likely to work correctly.

    Also, if you have files named obj or bin within the folder tree the command is working on, then you will recieve an error message for each file encounted that has a matching name. These matched files are not deleted, and should not get in the way of the command deleting what it should. In otherwords they can be ignored safely.