Wednesday, May 8, 2024
 Popular · Latest · Hot · Upcoming
9
rated 0 times [  9] [ 0]  / answers: 1 / hits: 1372  / 1 Year ago, wed, may 10, 2023, 2:52:27

I have a batch application on Windows that I run every day at midnight. Recently, I converted this into a Linux shell script on a Linux machine and would like to similarly run it every day at midnight. My requirements are as follows:-



  • For the shell script to run in the foreground - this is to allow me to see the output of the running process and correct/fix things if it fails.

  • Similar to how the Windows task scheduler works, I would like to have a list of scripts that I can run, and those lists can be enabled/disabled.

  • (Optional) Ideally, the Task Scheduler equivalent can be modified programmatically. This would allow me to disable/enable tasks based on the result of an already run task.


Is this doable? I've seen the links below but they don't contain all the criteria that I need for my use case. If one of the links below is the correct way to go then an example can be really helpful.



More From » bash

 Answers
3

To run a cron job at midnight, you would prefix your command or the path to your script with:


0 0 * * *

For example, to run /home/youssif/myscript , you would use:


0 0 * * /home/youssif/myscript

in your crontab file.




To edit your crontab file, use crontab -e to run the command as your current user. Alternatively, if you absolutely need to run the command as root, you can run sudo crontab -e to edit your crontab file.




To run the command in a terminal (not a good idea if you run the command as root), you will need to specify the display to use in your command or in your script. To do this, assuming your $DISPLAY is :0 (default) you can prefix your command with the following variable:


DISPLAY=:0

Also, a terminal will typically close after the command is executed but you can use the hold option with xterm to keep the terminal open.


So, to run echo "hello world" in a terminal at midnight, your command would look like this:


0 0 * * DISPLAY=:0 xterm -hold -e 'echo "hello world"'

or to run your script:


0 0 * * DISPLAY=:0 xterm -hold -e '/home/youssif/myscript'

However, the standard way to inspect the output of a cronjob is to redirect the terminal output to a file which you can look at later.


For example, to redirect the terminal output to the file /home/youssif/helloworld.log you would use the following line in your crontab file:


0 0 * * echo "hello world" > /home/youssif/helloworld.log

Alternatively, you can also use the tee command to redirect the output like this:


0 0 * * echo "hello world" | tee /home/youssif/helloworld.log

Finally, you can use the cat command to view the contents of the file:


cat /home/youssif/helloworld.log

This way, the command can run in the background but you can still inspect the output.




To disable/enable tasks based on the result of an already run task, I think this would really be more appropriate for a separate question. I believe your answer would involve using an "if then else" statement in a bash script.




EDIT:


As @Tcooper pointed out, we have to add 2>&1 to redirect all the output, including error messages so you might want to use something like this instead:


0 0 * * echo "hello world" 2>&1 > /home/youssif/helloworld.log

or


0 0 * * echo "hello world" 2>&1 | tee /home/youssif/helloworld.log

[#1241] Wednesday, May 10, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
fittecanap

Total Points: 322
Total Questions: 100
Total Answers: 105

Location: Israel
Member since Tue, Nov 17, 2020
4 Years ago
;