0

I am trying to set up a script that runs 2 mate-terminal instances, each with a different command.

The script is set up as a cronjob.

The problem is, while the script works fine when executed manually, in terminal, it fails to open the 2 terminal windows.

I tried different approaches, but nothing worked. I also have gnome-terminal installed, same result.

The script runs, kills the processes and their terminal but that's it.

LE: The script runs, it kills all the processes and terminal windows but it does not open the new terminal windows with the given commands.

This is an old application and the only logging is visible in the terminal, in real time. Since there is no plan for upgrading the application anytime soon, to export the log to an external file, this is the requirement.

Any ideas on what I am doing wrong?

#!/bin/bash export DISPLAY=:10.0 export XAUTHORITY=/home/lxadmin/.Xauthority export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin echo "Script started at $(date)" >> /home/lxadmin/Logs/MyApp.log close_running_terminals() { echo "Closing running terminals" >> /home/lxadmin/Logs/MyApp.log pkill -f "node" pkill -f "npm" for terminal in $(pgrep -f mate-terminal); do if ps -o args= -p $terminal | grep -qE 'node|npm'; then echo "Killing mate-terminal process: $terminal" >> /home/lxadmin/Logs/MyApp.log kill -9 $terminal fi done sleep 2 } open_terminal_and_run() { local cmd=$1 echo "Running command: $cmd" >> /home/lxadmin/Logs/MyApp.log /usr/bin/mate-terminal -- /bin/sh -c "$cmd; exec bash" #/usr/bin/mate-terminal -- bash -c "$cmd; exec bash" } close_running_terminals open_terminal_and_run "cd /home/lxadmin/Servers/MyApp/server && /usr/local/lib/nodejs/node-v12.13.1-linux-x64/bin/nodemon app.js" sleep 5 open_terminal_and_run "cd /home/lxadmin/Servers/MyApp/client && /usr/local/bin/npm run dev" echo "Script completed at $(date)" >> /home/lxadmin/Logs/MyApp.log 
17
  • 1
    How does "it fails" manifest itself? Commented Oct 21, 2024 at 16:11
  • 1
    This question is similar to: A basic Bash script (to start a GUI program) works partially in cron. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Oct 21, 2024 at 16:16
  • @tink I can see it terminates the node / npm processes as well as closing the terminal windows but does not open new terminal windows with the given command. Basically, open_terminal_and_run does not seem to work. Commented Oct 21, 2024 at 16:20
  • 1
    Please edit your question and include the information from the comments. Especially the part where you describe what the actual error is since that isn't visible from the question at all. Make sure to explain why you need a terminal for this. If the issue is logging, you can redirect output. If you explain the situation we might be able to help, but not with the lack of detail in the question. Commented Oct 21, 2024 at 16:33
  • 1
    @daydr3am3r - if this is an old application - what changed? And running it via RDP is a fixed requirement? Why? It would make so much more sense to run it under tmux or screen, and redirect its output to a file. Seeing output dash past in an RDP session is not exactly useful logging ... Commented Oct 21, 2024 at 16:48

1 Answer 1

0

Since Crontab does not work the way I wanted to and I thought it does, I decided to go with systemd and create a service and a timer to trigger the script at midnight.

  • Create the service file:
    nano ~/.config/systemd/user/RestartApp.service 
    [Unit] Description=Restart App via RestartApp.sh [Service] ExecStart=/path/to/RestartApp.sh 
  • Create the timer
    nano ~/.config/systemd/user/RestartApp.timer 
    [Unit] Description=Restart App Daily at 00:00 [Timer] OnCalendar=*-*-* 00:00:00 Persistent=true [Install] WantedBy=timers.target 
  • Enable the mechanism
    systemctl --user daemon-reload systemctl --user enable RestartApp.timer systemctl --user start RestartApp.timer 

Not sure if this is the correct way of achieving what I wanted but it works for me.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.