-1

I have the following tmux script which is executed successfully only when I navigate to /dev/shm (that is, after I do cd /dev/shm):

(
cat > /dev/shm/script.sh && 
chmod +x /dev/shm/script.sh && 
tmux new-session -d '/dev/shm/script.sh; rm /dev/shm/script.sh'
) <<-'EOF'
cd /var/www/html
wget SOME_FILE
sleep 2h
tmux kill-session
EOF

Why would the script be executed successfully only when I navigate to /dev/shm and not from any other place in the system?

  • 1
    What do you mean "when I execute it inside of /dev/shm"? Do you mean when the script itself is located in /dev/shm, or when you cd /dev/shm before executing the script? Also how does it not work? It's hard for us to troubleshoot without knowing what is failing. – Patrick May 1 '17 at 4:22
  • When I do cd /dev/shm. I've updated the question to clarify that... – JohnDoea May 1 '17 at 7:42
  • The first problem I see is that your script is not really executable even though you chmod +x it: it is missing a shebang line. It probably works only by luck and some fallback script execution logic in tmux or your shell. You could try fixing that first? The second anomaly I see is that you are supplying a heredoc as stdin to a sequence of three commands, although it is really only the first command (the cat) that uses it. Why not reduce the scope of that redirection? – Celada May 1 '17 at 8:52
  • I used to run a similar script many times without #!/bin/bash so what you say about luck/fallback sounds plausible to me. IIUC, you suggest to remove the parenthesis and use a shabang statement? – JohnDoea May 1 '17 at 9:02
  • I don't think I have enough Bash knowledge to fully understand why Mario Vitale suggested doing so when including a heredoc in the first place: unix.stackexchange.com/questions/335087/… – JohnDoea May 1 '17 at 9:03
-1

My problem was not using the command bash when executing the script like this:

tmux new-session -d 'bash /dev/shm/script.sh; rm /dev/shm/script.sh'

I now use a slightly "lighter" version of the script, which I find respectively easier to maintain, as in:

(
cat <<'PMA' > /opt/script.sh
#!/bin/bash
cd /var/www/html
wget SOME_FILE
sleep 2h
tmux kill-session
PMA
chmod +x /opt/script.sh
tmux new-session -d 'bash /opt/script.sh'
tmux new-session -d 'rm /opt/script.sh'
)

Note: In either version, ensure to replace all leading spaces to tabulations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.