Run a specific function from a file that contains 2 or more functions, in a detached tmux session Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Run a specific function from a file that contains 2 or more functions, in a detached tmux session without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I use Ubuntu 16.04 with Bash and I have a file that contains many different functions. In between these functions, there is this one:

tdm() {
    chmod -R a-x,a=rX,u+w "$drt"/phpmyadmin/
    sleep 2h
    chmod -R 000 "$drt"/phpmyadmin/
    tmux kill-session
}

This function exists because after I install phpmyadmin (PMA), I restrict it by chmod /usr/share/phpmyadmin 000 -R for security reasons, and the above function uses me to temporarily permit PMA, for 2 hours (2 hours is usually more than enough for me to do my changes in the database). Hence the name TDM (temporary Database Management).

Note that this function has no call. It has none because I call it directly from the CLI via executing tdm, after sourcing the file that contains it.


My problem

My problem is that the sleep command inside the function prevents me from using the console (any foreground change will stop sleep).

I thus need tdm to be executed in a detached tmux session, there it will change permissions to 755/644, sleep 2 hours (without interrupting my work in the console), then it will lock PMA again, and kill the detached tmux session.

The only reason I use tmux here is so that the sleep utility won’t interrupt the current session that I work with, as described above.


What I contemplated so far

1 – Using Bash background instead tmux:

I could remove the tmux part and call the function in the background with & but I don’t want that; I desire not to use any arguments when running the function.

2 – A dedicated file:

One way to run tdm in a detached tmux session is to put the subroutine in a script in a dedicated file, then creating an alias for that file, and run it directly:

1) Create a dedicated file:

$HOME/tdm.sh

2) Add an alias to "$HOME"/.bashrc:

alias tdm="tmux new-session -d 'bash $HOME/tdm.sh'"

3) Source bashrc:

source "$HOME"/.bashrc

4) use the alias:

tdm

Yet I don’t want to put the function in a script in a dedicated file; I want to keep it its current file that contains other functions.


My question

How could I run the function from inside the file containing other functions, in a detached tmux session?

Update

A disown or nohup solution might be good. As long as I run the function only with tdm without arguments, then I don’t care if it’s done with disown, nohup or tmux.

Answer

In the comments, the user Rich offered the following solution but desired not to publish an answer.

I understand he meant this:

tdm() {
    chmod -R a-x,a=rX,u+w "$drt"/phpmyadmin/
    echo "chmod -R 000 $drt/phpmyadmin/" | at now + 2 hours
}
We are here to answer your question about Run a specific function from a file that contains 2 or more functions, in a detached tmux session - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji