4

I am trying to run python script inside tmux session. I wrote a command (tmux new-session -d -s my_session) which is running fine from crontab.

But when I am trying to run python or shell file with tmux new-session -d -s my_session 'python3 test.py or tmux new-session -d -s my_session 'sh test.sh The script doesn't run. I used the reference from here. Please help me with this.

HK179
  • 43
  • 5
Hayat
  • 1,539
  • 4
  • 18
  • 32

1 Answers1

7

Edit
You can separate tmux commands with \;, then use the send-keys command to send the command to the active window.
In your case you can use:

tmux new-session -d -s my_session \; send-keys "python3 test.py" Enter
tmux new-session -d -s my_session \; send-keys "sh test.sh" Enter
tmux new-session -d -s my_session \; send-keys "python3 -m http.server 8080" Enter

You can find more about send-keys options on the tmux manpages section for send-keys:

send-keys [-lMRX] [-N repeat-count] [-t target-pane] key ...
(alias: send)
Send a key or keys to a window. Each argument key is the name of the key (such as ‘C-a’ or ‘NPage’) to send; if the string is not recognised as a key, it is sent as a series of characters. The -l flag disables key name lookup and sends the keys literally. All arguments are sent sequentially from first to last. The -R flag causes the terminal state to be reset.

-M passes through a mouse event (only valid if bound to a mouse key binding, see MOUSE SUPPORT).
-X is used to send a command into copy mode - see the WINDOWS AND PANES section.
-N specifies a repeat count.

The send-keys syntax is described on the Key Bindings section of the tmux manpage. The key names used by send-keys are the same ones used by bind-key.

I usually work with different configuration files, on top of a base file.

Imagine that you've your tmux configuration in ~/.tmux.conf I then create different configuration files in my ~/.tmux/ folder. As an example I can have a python configuration file (use the attach if you want to enter in the session):

# To use this configuration launch tmux with the command:
#   > tmux -f ~/.tmux/python.conf attach
#

# Load default tmux config
source-file ~/.tmux.conf

# Create session and launch python script
new-session -s python -n python -d -c ~/src/python/
send-keys "python test.py" Enter

This gives me the flexibility to create much more complex sessions.

pfmaggi
  • 6,116
  • 22
  • 43
  • I tried this tmux new-session -d -s myTempSession `sh runPython.sh` but this didn't work. And how about ~/.tmux.conf` how do I create it. Please guide. – Hayat Mar 17 '18 at 19:36
  • That's is your custom tmux configuration, if you're not using one, you can simply comment that line. – pfmaggi Mar 17 '18 at 20:18
  • Cool, But this back ticks "`" isn't working. Can you please check? – Hayat Mar 18 '18 at 04:49
  • Sorry, you're right the back ticks was definitely not working! I've corrected the answer using \; and the send-keys to execute the command in the newly created tmux session. Is this what are you looking for? – pfmaggi Mar 18 '18 at 10:40
  • That worked like a charm. Thanks a lot. Is there any reference where I can study about `send-keys` and other things about tmux and it's syntax? – Hayat Mar 19 '18 at 05:23
  • I've added few notes about send-keys syntax. – pfmaggi Mar 19 '18 at 07:04