2

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.

Arcticooling
  • 1
  • 12
  • 44
  • 103
  • Why not simply make your tmux command do `bash -c 'source file-with-all-functions; tdm'`? – meuh Feb 17 '18 at 08:52
  • I already sourced the file and the function is named `tdm` so I just want to run it with `tdm` (as I can), but I just need it to run in a detached `tmux` session. – Arcticooling Feb 17 '18 at 09:25
  • Why don't you just run `tdm &`? Send it to the background and you don't need to wait for it. Do you really need it in a tmux session? That seems like pointless overhead. – terdon Feb 17 '18 at 16:26
  • @terdon I want it to be done in another session without `&`, then I just run the function without special arguments... – Arcticooling Feb 17 '18 at 16:50
  • Please clarify why you need this to be done using tmux. There are various other way, you can use a subshell, send it to the background, disown, nohup, tmux is the most complex and resource intensive of all of them. – terdon Feb 17 '18 at 17:21
  • @terdon I tried to explain why it is important for me to use `tmux` though I'd might use `nohup` because in a function, it might be good. – Arcticooling Feb 17 '18 at 21:06
  • *The only reason I use tmux here is so that the sleep utility won't interrupt the current session that I work with* Okay, so drop the `sleep` and following `chmod` and use `echo "chmod -R 000 "$drt"/phpmyadmin/"|at +2h` in your function. This way you can just call the function *in the foreground*. Really this should be filed under "asking the wrong question". Go ahead someone else get the bounty I don't have time. – Rich Feb 22 '18 at 23:15

4 Answers4

2

Use at;

tdm() {
  chmod -R a-x,a=rX,u+w "$drt"/phpmyadmin/ && \
  echo "chmod -R 000 \"$drt\"/phpmyadmin/" | at "now + 2h"
}

or sleep with

nohup

tdm() {
  chmod -R a-x,a=rX,u+w "$drt"/phpmyadmin/ && \
  nohup "sleep 2h; chmod -R 000 \"$drt\"/phpmyadmin/" &
}

or screen

tdm() {
  chmod -R a-x,a=rX,u+w "$drt"/phpmyadmin/ && \
  screen -S tdm -d -m "sleep 2h; chmod -R 000 \"$drt\"/phpmyadmin/"
}

or tmux

tdm() {
  chmod -R a-x,a=rX,u+w "$drt"/phpmyadmin/ && \
  tmux new -d -s tdm "sleep 2h; chmod -R 000 \"$drt\"/phpmyadmin/"
}
user1133275
  • 5,488
  • 1
  • 19
  • 37
1

If I understand what you are trying to do, you're using a tool too powerful for the job. Instead of spawning a dedicated tmux session just for a single simple script, run that script from the command line using nohup & and disown.

nohup tdm DRT & disown
user1404316
  • 3,028
  • 12
  • 23
0

Not sure this is what you asked for, but you can directly pass in parameter the commands you want to run in the deteached session :

tmux new-session -n:myTest 'chmod -R a-x,a=rX,u+w "$drt"/phpmyadmin/;sleep 2h;chmod -R 000 "$drt"/phpmyadmin/'
Félicien
  • 493
  • 3
  • 9
  • I've edited the question to hopefully make it clearer. You are very welcome to review the question and edit accordingly. – Arcticooling Feb 16 '18 at 20:30
-1

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
}
Arcticooling
  • 1
  • 12
  • 44
  • 103
  • Both your answer and Rich's comment are 5 hours after the bonus receiving answer. If you want people to upvote your Q/A you should play nicer; give credit to the answer that was most correct and is not your own. – user1133275 Feb 27 '18 at 11:27
  • Did you downvote my answer? I don't track any update on any answer. I upvoted your answer and gave you bounty because at the time it seems the best but I don't recall it helped me and I might missed something. – Arcticooling Mar 03 '18 at 21:10