Generate a bash script which periodically monitors system resources usage of a particular process. It should accept parameters such as process id to monitor, delay in seconds, time period. It should store the observed system metrics into a file.
user
Sure, I can help you create a basic bash script. However, since you've mentioned that the process ID (PID) is a parameter, I'll assume that you'll be passing it through a command line argument.

Here's the basic bash script:

```bash
#!/bin/bash

# Check if all required parameters have been provided.
if [[ $# -lt 3 ]]; then
  echo "Not enough parameters provided. Please provide PID, delay, and time period as command line arguments."
  exit 1
fi

# Get process ID and delay from command line arguments.
pid=$1
delay=$2
time_period=$3

# Get current date and time in YYYY-MM-DD HH:MM:SS format.
current_timestamp=$(date +"%Y-%m-%d %H:%M:%S")

# Loop for specified time period.
while true; do
  # Get CPU usage, memory usage, and swap usage.
  cpu_usage=$(top -bn1 | grep -i $pid | sed "s/.*/\1% CPU\(s\+\) Ir\1 St\1 Ts\1 Id\1 Us\1 Sy\1\.*/\1/" | awk '{print 100 - $1"%"}')
  mem_usage=$(top -bn1 | grep -i $pid | sed "s/.*/\1%\ [Mem\/\(Mem+swap\/\)\ x\1\%\ x\1\ x\1\ x\1\]//" | awk '{print $3*100}')
  swap_usage=$(top -bn1 | grep -i $pid | sed "s/.*/Swap: \([^\ x\1].*\)/\1/g" | awk '/Mem|Swap/{printf "%i", $3*1024}')

  # Print and log the metrics.
  echo "[$current_timestamp] PID $pid CPU $cpu_usage%, Mem $mem_usage%, Swap $swap_usage kB"
  echo "[${current_timestamp}] PID $pid CPU $cpu_usage%, Mem $mem_usage%, Swap $swap_usage kB" >> monitor.log

  # Sleep for specified delay.
  sleep $delay

  # Check if time period is reached.
  if [[ $(date +"%Y-%m-%d %H:%M:%S" --date " + $delay seconds") > $current_timestamp ]]; then
    echo "Completed monitoring after $(date -d "$current_timestamp+$time_period seconds" +"%Y-%m-%d %H:%M:%S")"
    break
  fi
done

echo "Monitoring completed."
```

This script will run indefinitely until interrupted, but it will only print and log metrics when the specified delay is met every time, and finally exit only after the specified time period. Ensure that the user has permission to execute this shell script. Also, edit the path for this shell script to be executable by running `chmod +x my_shell_script.sh`. When you're done, save it.

You can execute it like this:

```bash
./monitor.sh 1234 10 3600
```

This command will run it for every 10 seconds, for 1 hour in total.

Please remember that this is a flawed basic script, which should be transformed into a more robust one to fit your needs.
assistant
Generate a bash script which periodically monitors system resources usage of a particular process. It should accept parameters such as process id to monitor, delay in seconds, time period. It should store the observed system metrics into a file.
user
Here's a Bash script that monitors system resources usage for a particular process and stores the metrics in a file. You can customize the process ID, delay in seconds, and time period as needed.

```bash
#!/bin/bash

# Check if the required parameters are provided
if [ "$#" -ne 3 ]; then
    echo "Usage: $0 [process_id] [delay_seconds] [total_time]"
    exit 1
fi

# Assign input parameters to variables
process_id=$1
delay=$2
total_time=$3
end_time=$(( $(date +%s) + total_time ))
output_file="process_monitor_${process_id}.log"

# Validate process ID
if ! ps -p ${process_id} > /dev/null; then
    echo "Error: Process ID ${process_id} not found."
    exit 1
fi

echo "Monitoring system resources usage for process ID ${process_id}..."
echo "Delay: ${delay} seconds"
echo "Total Time: ${total_time} seconds"
echo "Output File: ${output_file}"

# Monitor system resources usage periodically
echo "Timestamp,CPU%,MEM%,VSZ,RSS" > ${output_file}
while [ $(date +%s) -lt ${end_time} ]; do
    cpu_usage=$(ps -p ${process_id} -o %cpu --no-headers)
    mem_usage=$(ps -p ${process_id} -o %mem --no-headers)
    vsz=$(ps -p ${process_id} -o vsz --no-headers | awk '{sum=0; for(i=1; i<=NF; i++) sum+=$i; print sum/1024}')
    rss=$(ps -p ${process_id} -o rss --no-headers | awk '{sum=0; for(i=1; i<=NF; i++) sum+=$i; print sum/1024}')

    echo "$(date),${cpu_usage},${mem_usage},${vsz},${rss}" >> ${output_file}
    sleep ${delay}
done

echo "Monitoring completed. Data stored in ${output_file}"
```

To run the script, save it into a file (e.g., `monitor_process.sh`), give it execute permissions with `chmod +x monitor_process.sh`, and then execute it with the process ID, delay, and total time as arguments:

```bash
./monitor_process.sh [process_id] [delay_seconds] [total_time]
```

For example:

```bash
./monitor_process.sh 1234 5 60
```

This would monitor the process with ID 1234, taking measurements every 5 seconds for a total of 60 seconds. The results will be stored in a file named `process_monitor_1234.log`.
assistant