On my development setup I have an active session with multiple windows, I'd like to spit out the cronjob's output to this active session into a specific window .
I wrote in the crontab the command:

/bin/bash /home/user/backup.sh

so the backup script looks quite straightforward:

# Setting up all environment variables:
export LC_ALL=C.UTF-8
export LANG=C.UTF-8
export PATH=$PATH:/usr/local/bin/
export DISPLAY=:0;

tmux new-session -d -s <session-name> -n <window name>
tmux send-keys "/usr/bin/rsync /home/user/input /home/user/output/" ENTER

BUT the script writes the output to the currently active window in tmux, how do i make sure to output into window name i mentioned in the script?

Thank you all!

  • Can you show us the cron command in question (ie. the crontab entry), and tell us where it normally sends its output?
    – JigglyNaga
    Dec 21 '18 at 11:59
  • @JigglyNaga I have elaborated, as you requested :) any suggestions? Dec 21 '18 at 23:47
  • tmux new-session starts a new session, but you're asking how to put the new process into an already-active session. Which do you actually want? What should happen if there isn't an active session when the cron job runs?
    – JigglyNaga
    Dec 22 '18 at 0:15
  • The session always exist from the moment I login into my system. i'd like to attach to the already-active session. is this the correct command in tmux? Dec 22 '18 at 0:43

It sounds like you're looking for tmux new-window, with the -d option. From tmux(1):

new-window [-adkP ] [-c start-directory ] [-F format ] [-n window-name ] [-t target-window ] [shell-command ]


If -d is given, the session does not make the new window the current window.

So your script will need the single command:

tmux new-window -d -n <new_window_name> -t <existing_session_name>: /usr/bin/rsync ...

Note that there is a : after the session name. For the new-window command, the -t option expects a target window (in the form session:window), not just a target session.

  • I have tried it on my system (I have Tmux 2.7 installed), and unfortunately it doesn't work :/ Do i need the sendkeys command too? I tried both and it failed :/ Dec 22 '18 at 10:20
  • I've tested with 2.7 and 2.8. Can you please expand on "doesn't work"? Try running the command directly, without a script/cron, and see what error it gives.
    – JigglyNaga
    Dec 22 '18 at 10:39

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.