Setup a cron for multiple commands in a single line - unix

I execute a sh file using command ./abc.sh abc_config
How do we get this done via cron ? for a single command i usually do
5 0 * * * /test/abc.sh
But for ./abc.sh abc_config I am a little confused

One option could be to store the script and it's argument in another script and run that instead?
5 0 * * * /test/run_abc.sh
Contents of run_abc.sh
./abc.sh abc_config
You should possibly include paths and a "shebang" matching whatever shell you want to use, ie:
#!/usr/bin/env bash
/path-to-dir-where-program-is/abc.sh abc_config

Related

Sourcing Alias Prevents Less Buffer From Displaying

I'm writing a bash script that needs to both be able to cd in the current shell and use less to display longform text. To be able to cd, I understand that I need to source the script when I call it, which I've done via an alias in my ZSH config. However, when I do this, less breaks: instead of echo -e "$result" | less displaying its usual scrolling buffer, the long text gets dumped into the shell.
For context, this is a bash script acting as a wrapper for a Node.js script so as to be able to have native access to bash commands (like cd, open, etc.). The alias in my zshrc is as follows (with the path truncated): alias bk='source ~/.../bookmark/bookmark.sh'.
Is there any way to satisfy both the need to cd and the need to use less?
Fixed! This turned out to be an issue in my script's logic. I was using condition=$(echo $result | cut -c 1-3), but in reality need the first three characters (not columns) of $result, which I then did by using $result | head -c 3. What's interesting about this is that fetching the first three columns from $result when determined by running ./bookmark.sh works as an equivalent to fetching the first three characters, but running the alias yields the issue here.

What does * means in the list from the ls command?

Why do .sh files have an * next to them?
It means that the file is executable.
Reference: General Output Formatting
The * means that the file is executable

Is it possible to use wild characters to delete dataset on z/OS

I want to remove lots of temporary PS datasets with dataset name like MYTEST.**, but still can't find an easy way to handle the task.
I meant to use a Shell command below to remove them
cat "//'dataset.list'"| xargs -I '{}' tsocmd "delete '{}'"
However, first I have to save the dataset list into a PS dataset or Unix file. In Unix, we can redirect output of ls command into a text file: "ls MYTEST.* > dslist", but on TSO or ISPF panel, seems no simple command to do that.
Anyone has any clue on this? Your comment would be appreciated.
Rexx ISPF option is probably the easiest and can be used in the future, but options include:
Use the save command in ispf 3.4 to save to a file, then use a rexx program on the file created by the save command
listcat command, in particular
listcat lvl(MYTEST) ofile(ddname)
then write a rexx program to do the actual delete
Alternatively you can use the ISPF services LMDINIT, LMDLISTY & LMDFREE in a rexx program running under ISPF i.e.
/* Rexx ispf program to process datasets */
Address ispexec
"LMDINIT LISTID(lidv) LEVEL(MYTEST)"
"LMDLIST LISTID("lidv") OPTION(list) dataset(dsvar) stats(yes)"
do while rc = 0
/* Delete or whatever */
end
"LMDFREE LISTID("lidv")"
For all these methods you need to fully qualify the first High level qualifier.
Learning what Rexx / ISPF will serve you into the future. In the ISPF Editor, you can use the model command to get Templates / information for all the ISPF commands:
Command ====> Model LMDINIT
will add a template for the lmdinit command. There are templates for rexx, cobol, pl1, ISPF-panels, ISPF-skeletons messages etc.
Thanks Bruce for the comprehensive answer. According to Bruce's tips, I just worked out a one-line Shell command as below:
tsocmd "listcat lvl(MYTEST) " | grep -E "MYTEST(\..+)+" | cut -d' ' -f3 | xargs -I '{}' tsocmd "delete '{}'"
Above command works perfectly.
Update - The IDCAMS DELETE command has had the MASK operand for a while. You use it like:
DELETE 'MYTEST.**' MASK
Documentation for z/OS 2.1 is here.

Crontab to create a new file

I'm trying to create a simple crontab that creates a file called log.txt every minute by populating it with a simple command's output. Right now this is what I've put into my crontab:
* * * * * (/usr/bin/ls <pathToRandomDirectory) > log.txt
By my understanding, the 5 asterisks correspond to "every minute". But when I run this the log.txt file is not being created. Is there something I'm missing here?
ALSO, if I didn't want to have an email sent to me whenever the job is created I found that I need to put the line:
>/dev/null 2>&1
Somewhere in my crontab file. Where exactly does this go? At the end of the command or on a separate line?
You have to put an absolute path for log.txt. Otherwise, it will be created in /.
Also, >/dev/null 2>&1 has to be at the end of the sentence. If you want the 2 (meaning the errors) to be dismissed, just write 2>/dev/null.
Then, your final cronjob would be like this:
* * * * * /usr/bin/ls pathToRandomDirectory > /pathToRandomDirectory/log.txt 2>/dev/null
What exactly should be in that file? This creates a new file with the text "something" in it every minute:
* * * * * echo "something" > /path/to/your/file.txt
I had some problems with crontab especially when I created a new crontab config - my problem was that I had to insert an empty line at the end of the crontab - some systems need it to work correctly. So if you do
$ crontab -l
the output should be a list of your cron jobs on each line and then an empty line at the end.
Try it if it works for you. Sorry, but I'm not sure about the email, but I dont think that you need to put lines like that to crontab, play around with the configuration - it shouldn't send you any emails by default.

Cron fails on single apostrophe

The following does work as expected:
date +'%d-%b-%Y-%H-%M'
28-Sep-2009-14-28
But none of the following 4 entries from crontab are working.
* * * * * date +\'%d-%b-%Y-%H-%M\' >> /backup/shantanu/testing.txt
* * * * * date +'%d-%b-%Y-%H-%M' >> /backup/shantanu/testing1.txt
* * * * * date +"%d-%b-%Y-%H-%M" >> /backup/shantanu/testing2.txt
* * * * * date +\"%d-%b-%Y-%H-%M\" >> /backup/shantanu/testing3.txt
Error:
/bin/sh: -c: line 0: unexpected EOF while looking for matching `"'
/bin/sh: -c: line 1: syntax error: unexpected end of file
I can save the same code in a shell script and set the cron, but I will like to know if it is possible to directly set a cron for the task.
The actual cron entry that I am trying to set looks something like this...
16 * * * * mysqldump myDB myTB > /backup/ABCbc$(date +'%d-%b-%Y-%H-%M').sql 2> /backup/ABCbc_errORS$(date +'%d-%b-%Y-%H-%M').txt
There are four common causes for cron job commands to behave differently compared to commands typed directly into an interactive shell:
Cron provides a limited environment, e.g., a minimal $PATH, and other expected variables missing.
Cron invokes /bin/sh by default, whereas you may be using some other shell interactively.
Cron treats the % character specially (it is turned into a newline in the command).
The command may behave differently because it doesn't have a terminal available.
You must precede all % characters with a \ in a crontab file, which tells cron to just put a % in the command, e.g.
16 * * * * mysqldump myDB myTB > "/backup/ABCbc$(date +'\%d-\%b-\%Y-\%H-\%M').sql" 2> "/backup/ABCbc_errORS$(date +'\%d-\%b-\%Y-\%H-\%M').txt"
(As a separate matter, always put double quotes around a "$variable_substitution" or a "$(command substitution)", unless you know why not do it in a particular case. Otherwise, if the variable contents or command output contains whitespace or ?*\[, they will be interpreted by the shell.)
As long as there are no spaces in the format string supplied as an argument to date, you should not need the ticks at all.
date +%d-%b-%Y-%H-%M
should work.
You're using a syntax not supported by /bin/sh. Try invoking your preferred shell and passing the command as an argument.

Resources