Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7683095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:43:04+00:00 2026-05-31T18:43:04+00:00

I need to execute a bash script on boot. To do so I created

  • 0

I need to execute a bash script on boot.
To do so I created a file

/etc/init.d/blah

I edited it and added the following lines

#! /bin/sh
# /etc/init.d/blah

touch '/var/lock/blah'
username1=$(id -n -u)
username2=$(whoami)
touch '/var/lock/1'${username1}
touch '/var/lock/2'${username2}

exit 0

The script is execute with root privileges (which is what I need because I have to use mount inside this script) .. but the problem is that I also need to know the username of the user who has just logged-in beacuse my goal is to mount a certain folder to a certain mount-point depending on the username, like

mount -o bind /home/USERNAME/mount-point /media/data/home/USERNAME/to-be-mounted

Going back to the boot script, if I do

sudo update-rc.d blah defaults

and then reboot and log-in with my username (let’s say john) both ways to get username in my script produce root in fact I’ve got 3 files

/var/lock/blah
/var/lock/1root
/var/lock/2root

So, how can I get the username of the user who just logged-in? (john in my example)

EDITED:
I solved in this way:

1. I created a .desktop file for each user I need to perform automount on boot to autostart a script on boot (I’m on LXDE) and put it on /home/{username}/.config/autostart

[Desktop Entry] 
Type=Application
Exec=bash "/path/to/mount-bind.sh"

2. I stored in that path a bash script called mount-bind.sh and made it executable:

#!/bin/bash

_username=$1

if [[ -z "${_username}" ]]; then
    _username="$(id -u -n)"
fi

mkdir -p "/home/${_username}/mount-folder"
sudo mount -o bind "/media/data/home/${_username}/mount-folder" "/home/${_username}/mount-folder"

exit 0

3. I added the following line to /etc/sudoers

%nopwd ALL=(ALL) NOPASSWD: /bin/mount

4. I created the nopwd group and added to it all the users I need

In his way after login I can mount the path under the user home.

Problem with this method is that I have to create the desktop file for each new user and add him/her to nopwd, but it works.

Any further improvement is welcome! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:43:05+00:00Added an answer on May 31, 2026 at 6:43 pm

    I think you should move from a boot time init script to a script executed at login time under the logged-in user. To allow this, you should look into ways to allow your users to execute the mount command you need. Depending on what you are trying to achieve, one of the following methods may help you:

    • Assuming you are on Linux or some other UNIX with a similar feature, add the mountpoint to /etc/fstab with the user option, allowing normal users to mount the entry.
    • Execute mount through sudo with a suitably narrow sudoers configuration as to not allow users to execute any mount commands.
    • Write a suid-root program in c which executes the required mount commands when called. This however is very tricky to get right without creating gaping security holes.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I execute the following bash script: #!/bin/bash version=$1 echo $version sed 's/\${version.number}/$version/' template.txt >
I am working on a bash script where I need to conditionally execute some
I need to have a script execute (bash or perl or php, any will
Do lines in a bash script execute sequentially? I can't see any reason why
I need to execute a Bash command in a Ruby script. There are about
I need to execute a PowerShell script from within C#. The script needs commandline
I need to execute a script in the background through a service. The service
i have this problem: I have created a bash script that performs some tasks.
I need to execute a Python script from an already started Python session, as
I created a bash script to thumbnail all images in a tree. It is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.