0

I am trying to convert a Windows batch file into bash shell scripting. But I am not able to understand how the following command can be converted. I have used wine to run .exe file in Linux. Is there any other way to run any .exe file in Linux?

The batch code which i want to convert in shell script is as follows:

echo Deleting any task..
call schtasks.exe /delete /TN "Task_name" /F

echo Creating new task..
call schtasks.exe /create /RU %computerName% \%UserName% /RP %Password% /TN 
"Task_name" /XML  "DATA/Task_name.xml"
5
  • 6
    You don't. Linux doesn't have a "registry" in any kind of meaningful sense, and your entire question is meaningless. I think we're dealing with an XY problem here, so perhaps rephrase your question and describe what you're hoping to accomplish with this -- it seems like what you want is to have something particular task executed on a schedule, which works quite a bit differently in linux. Jan 4, 2021 at 10:26
  • 4
    For us who have never really used Windows in this way, you may want to describe exactly what this is doing and what you want to do on Linux. I don't think you actually want to run schedtasks.exe on Linux, but do the equivalent things to what it is doing. Since we have no clue about what schedtasks.exe is, you need to tell us.
    – Kusalananda
    Jan 4, 2021 at 11:00
  • @Kusalananda I'm guessing that schedtasks is the CLI interface to the windows scheduler, and DATA/Task_name.xml is an XML file containing the specifics for the task they want scheduled. I'm also guessing that the OP has no idea what any of this is actually doing or they'd be asking something entirely different. Jan 4, 2021 at 11:08
  • @Shadur yeah.you are right. i am confused how can i create or scheduled and delete any task using shell scripting.I want to convert the same instruction what schetasks do in shell script.
    – Rohit Das
    Jan 4, 2021 at 11:11
  • You still don't because that's still not how the linux scheduler works. Please rephrase your question entirely and explain what exactly you're trying to accomplish rather than asking how to do something that likely won't accomplish that. Jan 4, 2021 at 11:12

1 Answer 1

2

I believe what the OP is trying to do is to create a scheduled task on Linux from a shell script.

Exactly what task and why is not even remotely clear from what they have posted, but generally this can be achieved by automating "crontab" from within a shell script.

Doing this requires some relatively advanced shell scripting techniques, so is probably not recommended for a novice at shell scripting.

An example of how this can be done is:

#!/bin/bash
# Create empty temporary file with safe name
TEMPFILE=$(mktemp)

# Put current crontab for user into the temporary file
crontab -l > $TEMPFILE

# Append new command to end of the created file using a shell 'here document'
cat << EOF >> $TEMPFILE
0 0 * * * /bin/df > /tmp/df-date-$(date +%d%b%Y)
EOF

# Load the new extended file in as the crontab for the user
crontab $TEMPFILE

This is just an example, not something that should be copied directly - use it to investigate each of the concepts being demonstrated and understand them.

There may possibly be issues if the user doesn't already have a crontab file. According to the man 5 crontab manual page on my system, another issue is some Linux versions of crontab -l add a header line which means that crontab -l | crontab - (i.e. trying to export and reimport the same data) will keep adding extra header lines.

There is also a race condition present - it is possible for the crontab for the user to be edited by another process between the crontab -l line and the line at the end to re-import the crontab. But I have seen things simular to this process used successfully in live environments.

For the meaning of the line 0 0 * * * /bin/df > /tmp/df-date-$(date +%d%b%Y), refer to the documentation for cron and crontab. The effect of my example will be to create a file in /tmp with the output of the df command from the system once per day.

4
  • 3
    Regarding your example command: Remember to escape the % characters as they are special within crontab schedules.
    – Kusalananda
    Jan 4, 2021 at 11:37
  • 1
    Instead of modifying general crontab with crontab command, it may be easier just add a specific crontab file for that specific task into /etc/cron.d directory
    – raj
    Jan 4, 2021 at 12:11
  • @raj That depends on whether he wants something done at superuser level or as a nonprivileged user. Jan 4, 2021 at 16:04
  • @Michael Firth Thanks a lot
    – Rohit Das
    Jan 5, 2021 at 5:00

Not the answer you're looking for? Browse other questions tagged .